Revert:PR c++/50800
[official-gcc.git] / gcc / ipa-devirt.c
blob7214a74b8e8cdc66891e5ca53d36d09c76891592
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"
169 #include "streamer-hooks.h"
170 #include "lto-streamer.h"
172 /* Hash based set of pairs of types. */
173 typedef struct
175 tree first;
176 tree second;
177 } type_pair;
179 struct pair_traits : default_hashset_traits
181 static hashval_t
182 hash (type_pair p)
184 return TYPE_UID (p.first) ^ TYPE_UID (p.second);
186 static bool
187 is_empty (type_pair p)
189 return p.first == NULL;
191 static bool
192 is_deleted (type_pair p ATTRIBUTE_UNUSED)
194 return false;
196 static bool
197 equal (const type_pair &a, const type_pair &b)
199 return a.first==b.first && a.second == b.second;
201 static void
202 mark_empty (type_pair &e)
204 e.first = NULL;
208 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
209 hash_set<type_pair,pair_traits> *);
211 static bool odr_violation_reported = false;
214 /* Pointer set of all call targets appearing in the cache. */
215 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
217 /* The node of type inheritance graph. For each type unique in
218 One Definition Rule (ODR) sense, we produce one node linking all
219 main variants of types equivalent to it, bases and derived types. */
221 struct GTY(()) odr_type_d
223 /* leader type. */
224 tree type;
225 /* All bases; built only for main variants of types. */
226 vec<odr_type> GTY((skip)) bases;
227 /* All derived types with virtual methods seen in unit;
228 built only for main variants of types. */
229 vec<odr_type> GTY((skip)) derived_types;
231 /* All equivalent types, if more than one. */
232 vec<tree, va_gc> *types;
233 /* Set of all equivalent types, if NON-NULL. */
234 hash_set<tree> * GTY((skip)) types_set;
236 /* Unique ID indexing the type in odr_types array. */
237 int id;
238 /* Is it in anonymous namespace? */
239 bool anonymous_namespace;
240 /* Do we know about all derivations of given type? */
241 bool all_derivations_known;
242 /* Did we report ODR violation here? */
243 bool odr_violated;
244 /* Set when virtual table without RTTI previaled table with. */
245 bool rtti_broken;
248 /* Return TRUE if all derived types of T are known and thus
249 we may consider the walk of derived type complete.
251 This is typically true only for final anonymous namespace types and types
252 defined within functions (that may be COMDAT and thus shared across units,
253 but with the same set of derived types). */
255 bool
256 type_all_derivations_known_p (const_tree t)
258 if (TYPE_FINAL_P (t))
259 return true;
260 if (flag_ltrans)
261 return false;
262 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
263 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
264 return true;
265 if (type_in_anonymous_namespace_p (t))
266 return true;
267 return (decl_function_context (TYPE_NAME (t)) != NULL);
270 /* Return TRUE if type's constructors are all visible. */
272 static bool
273 type_all_ctors_visible_p (tree t)
275 return !flag_ltrans
276 && symtab->state >= CONSTRUCTION
277 /* We can not always use type_all_derivations_known_p.
278 For function local types we must assume case where
279 the function is COMDAT and shared in between units.
281 TODO: These cases are quite easy to get, but we need
282 to keep track of C++ privatizing via -Wno-weak
283 as well as the IPA privatizing. */
284 && type_in_anonymous_namespace_p (t);
287 /* Return TRUE if type may have instance. */
289 static bool
290 type_possibly_instantiated_p (tree t)
292 tree vtable;
293 varpool_node *vnode;
295 /* TODO: Add abstract types here. */
296 if (!type_all_ctors_visible_p (t))
297 return true;
299 vtable = BINFO_VTABLE (TYPE_BINFO (t));
300 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
301 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
302 vnode = varpool_node::get (vtable);
303 return vnode && vnode->definition;
306 /* Hash used to unify ODR types based on their mangled name and for anonymous
307 namespace types. */
309 struct odr_name_hasher
311 typedef odr_type_d *value_type;
312 typedef union tree_node *compare_type;
313 static inline hashval_t hash (const odr_type_d *);
314 static inline bool equal (const odr_type_d *, const tree_node *);
315 static inline void remove (odr_type_d *);
318 /* Has used to unify ODR types based on their associated virtual table.
319 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
320 only polymorphic types. Types with mangled names are inserted to both. */
322 struct odr_vtable_hasher:odr_name_hasher
324 static inline hashval_t hash (const odr_type_d *);
325 static inline bool equal (const odr_type_d *, const tree_node *);
328 /* Return type that was declared with T's name so that T is an
329 qualified variant of it. */
331 static inline tree
332 main_odr_variant (const_tree t)
334 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
335 return TREE_TYPE (TYPE_NAME (t));
336 /* Unnamed types and non-C++ produced types can be compared by variants. */
337 else
338 return TYPE_MAIN_VARIANT (t);
341 static bool
342 can_be_name_hashed_p (tree t)
344 return (!in_lto_p || type_in_anonymous_namespace_p (t)
345 || (TYPE_NAME (t) && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))));
348 /* Hash type by its ODR name. */
350 static hashval_t
351 hash_odr_name (const_tree t)
353 gcc_checking_assert (main_odr_variant (t) == t);
355 /* If not in LTO, all main variants are unique, so we can do
356 pointer hash. */
357 if (!in_lto_p)
358 return htab_hash_pointer (t);
360 /* Anonymous types are unique. */
361 if (type_in_anonymous_namespace_p (t))
362 return htab_hash_pointer (t);
364 gcc_checking_assert (TYPE_NAME (t)
365 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
366 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
369 /* Return the computed hashcode for ODR_TYPE. */
371 inline hashval_t
372 odr_name_hasher::hash (const odr_type_d *odr_type)
374 return hash_odr_name (odr_type->type);
377 static bool
378 can_be_vtable_hashed_p (tree t)
380 /* vtable hashing can distinguish only main variants. */
381 if (TYPE_MAIN_VARIANT (t) != t)
382 return false;
383 /* Anonymous namespace types are always handled by name hash. */
384 if (type_in_anonymous_namespace_p (t))
385 return false;
386 return (TREE_CODE (t) == RECORD_TYPE
387 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
390 /* Hash type by assembler name of its vtable. */
392 static hashval_t
393 hash_odr_vtable (const_tree t)
395 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
396 inchash::hash hstate;
398 gcc_checking_assert (in_lto_p);
399 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
400 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
401 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
402 gcc_checking_assert (main_odr_variant (t) == t);
404 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
406 add_expr (TREE_OPERAND (v, 1), hstate);
407 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
410 hstate.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
411 return hstate.end ();
414 /* Return the computed hashcode for ODR_TYPE. */
416 inline hashval_t
417 odr_vtable_hasher::hash (const odr_type_d *odr_type)
419 return hash_odr_vtable (odr_type->type);
422 /* For languages with One Definition Rule, work out if
423 types are the same based on their name.
425 This is non-trivial for LTO where minor differences in
426 the type representation may have prevented type merging
427 to merge two copies of otherwise equivalent type.
429 Until we start streaming mangled type names, this function works
430 only for polymorphic types.
432 When STRICT is true, we compare types by their names for purposes of
433 ODR violation warnings. When strict is false, we consider variants
434 equivalent, becuase it is all that matters for devirtualization machinery.
437 bool
438 types_same_for_odr (const_tree type1, const_tree type2, bool strict)
440 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
442 type1 = main_odr_variant (type1);
443 type2 = main_odr_variant (type2);
444 if (!strict)
446 type1 = TYPE_MAIN_VARIANT (type1);
447 type2 = TYPE_MAIN_VARIANT (type2);
450 if (type1 == type2)
451 return true;
453 if (!in_lto_p)
454 return false;
456 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
457 on the corresponding TYPE_STUB_DECL. */
458 if (type_in_anonymous_namespace_p (type1)
459 || type_in_anonymous_namespace_p (type2))
460 return false;
463 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
465 Ideally we should never need types without ODR names here. It can however
466 happen in two cases:
468 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
469 Here testing for equivalence is safe, since their MAIN_VARIANTs are
470 unique.
471 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
472 establish precise ODR equivalency, but for correctness we care only
473 about equivalency on complete polymorphic types. For these we can
474 compare assembler names of their virtual tables. */
475 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
476 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
478 /* See if types are obviously different (i.e. different codes
479 or polymorphic wrt non-polymorphic). This is not strictly correct
480 for ODR violating programs, but we can't do better without streaming
481 ODR names. */
482 if (TREE_CODE (type1) != TREE_CODE (type2))
483 return false;
484 if (TREE_CODE (type1) == RECORD_TYPE
485 && (TYPE_BINFO (type1) == NULL_TREE)
486 != (TYPE_BINFO (type1) == NULL_TREE))
487 return false;
488 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
489 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
490 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
491 return false;
493 /* At the moment we have no way to establish ODR equivalence at LTO
494 other than comparing virtual table pointers of polymorphic types.
495 Eventually we should start saving mangled names in TYPE_NAME.
496 Then this condition will become non-trivial. */
498 if (TREE_CODE (type1) == RECORD_TYPE
499 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
500 && BINFO_VTABLE (TYPE_BINFO (type1))
501 && BINFO_VTABLE (TYPE_BINFO (type2)))
503 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
504 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
505 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
506 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
507 return (operand_equal_p (TREE_OPERAND (v1, 1),
508 TREE_OPERAND (v2, 1), 0)
509 && DECL_ASSEMBLER_NAME
510 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
511 == DECL_ASSEMBLER_NAME
512 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
514 gcc_unreachable ();
516 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
517 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
520 /* Return true if we can decide on ODR equivalency.
522 In non-LTO it is always decide, in LTO however it depends in the type has
523 ODR info attached.
525 When STRICT is false, compare main variants. */
527 bool
528 types_odr_comparable (tree t1, tree t2, bool strict)
530 return (!in_lto_p
531 || (strict ? main_odr_variant (t1) == main_odr_variant (t2)
532 : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
533 || (odr_type_p (t1) && odr_type_p (t2))
534 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
535 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
536 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
537 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
540 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
541 known, be conservative and return false. */
543 bool
544 types_must_be_same_for_odr (tree t1, tree t2)
546 if (types_odr_comparable (t1, t2))
547 return types_same_for_odr (t1, t2);
548 else
549 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
552 /* Compare types T1 and T2 and return true if they are
553 equivalent. */
555 inline bool
556 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
558 tree t1 = o1->type;
560 gcc_checking_assert (main_odr_variant (t2) == t2);
561 gcc_checking_assert (main_odr_variant (t1) == t1);
562 if (t1 == t2)
563 return true;
564 if (!in_lto_p)
565 return false;
566 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
567 on the corresponding TYPE_STUB_DECL. */
568 if (type_in_anonymous_namespace_p (t1)
569 || type_in_anonymous_namespace_p (t2))
570 return false;
571 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
572 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
573 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
574 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
577 /* Compare types T1 and T2 and return true if they are
578 equivalent. */
580 inline bool
581 odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
583 tree t1 = o1->type;
585 gcc_checking_assert (main_odr_variant (t2) == t2);
586 gcc_checking_assert (main_odr_variant (t1) == t1);
587 gcc_checking_assert (in_lto_p);
588 t1 = TYPE_MAIN_VARIANT (t1);
589 t2 = TYPE_MAIN_VARIANT (t2);
590 if (t1 == t2)
591 return true;
592 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
593 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
594 return (operand_equal_p (TREE_OPERAND (v1, 1),
595 TREE_OPERAND (v2, 1), 0)
596 && DECL_ASSEMBLER_NAME
597 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
598 == DECL_ASSEMBLER_NAME
599 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
602 /* Free ODR type V. */
604 inline void
605 odr_name_hasher::remove (odr_type_d *v)
607 v->bases.release ();
608 v->derived_types.release ();
609 if (v->types_set)
610 delete v->types_set;
611 ggc_free (v);
614 /* ODR type hash used to look up ODR type based on tree type node. */
616 typedef hash_table<odr_name_hasher> odr_hash_type;
617 static odr_hash_type *odr_hash;
618 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
619 static odr_vtable_hash_type *odr_vtable_hash;
621 /* ODR types are also stored into ODR_TYPE vector to allow consistent
622 walking. Bases appear before derived types. Vector is garbage collected
623 so we won't end up visiting empty types. */
625 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
626 #define odr_types (*odr_types_ptr)
628 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
629 void
630 set_type_binfo (tree type, tree binfo)
632 for (; type; type = TYPE_NEXT_VARIANT (type))
633 if (COMPLETE_TYPE_P (type))
634 TYPE_BINFO (type) = binfo;
635 else
636 gcc_assert (!TYPE_BINFO (type));
639 /* Compare T2 and T2 based on name or structure. */
641 static bool
642 odr_subtypes_equivalent_p (tree t1, tree t2,
643 hash_set<type_pair,pair_traits> *visited)
645 bool an1, an2;
647 /* This can happen in incomplete types that should be handled earlier. */
648 gcc_assert (t1 && t2);
650 t1 = main_odr_variant (t1);
651 t2 = main_odr_variant (t2);
652 if (t1 == t2)
653 return true;
655 /* Anonymous namespace types must match exactly. */
656 an1 = type_in_anonymous_namespace_p (t1);
657 an2 = type_in_anonymous_namespace_p (t2);
658 if (an1 != an2 || an1)
659 return false;
661 /* For ODR types be sure to compare their names.
662 To support -wno-odr-type-merging we allow one type to be non-ODR
663 and other ODR even though it is a violation. */
664 if (types_odr_comparable (t1, t2, true))
666 if (!types_same_for_odr (t1, t2, true))
667 return false;
668 /* Limit recursion: If subtypes are ODR types and we know
669 that they are same, be happy. */
670 if (!get_odr_type (t1, true)->odr_violated)
671 return true;
674 /* Component types, builtins and possibly violating ODR types
675 have to be compared structurally. */
676 if (TREE_CODE (t1) != TREE_CODE (t2))
677 return false;
678 if ((TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
679 return false;
681 type_pair pair={t1,t2};
682 if (TYPE_UID (t1) > TYPE_UID (t2))
684 pair.first = t2;
685 pair.second = t1;
687 if (visited->add (pair))
688 return true;
689 return odr_types_equivalent_p (t1, t2, false, NULL, visited);
692 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
693 violation warnings. */
695 void
696 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
698 int n1, n2;
700 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
702 odr_violation_reported = true;
703 if (DECL_VIRTUAL_P (prevailing->decl))
705 varpool_node *tmp = prevailing;
706 prevailing = vtable;
707 vtable = tmp;
709 if (warning_at (DECL_SOURCE_LOCATION
710 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
711 OPT_Wodr,
712 "virtual table of type %qD violates one definition rule",
713 DECL_CONTEXT (vtable->decl)))
714 inform (DECL_SOURCE_LOCATION (prevailing->decl),
715 "variable of same assembler name as the virtual table is "
716 "defined in another translation unit");
717 return;
719 if (!prevailing->definition || !vtable->definition)
720 return;
722 /* If we do not stream ODR type info, do not bother to do useful compare. */
723 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
724 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
725 return;
727 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
729 if (class_type->odr_violated)
730 return;
732 for (n1 = 0, n2 = 0; true; n1++, n2++)
734 struct ipa_ref *ref1, *ref2;
735 bool end1, end2;
737 end1 = !prevailing->iterate_reference (n1, ref1);
738 end2 = !vtable->iterate_reference (n2, ref2);
740 /* !DECL_VIRTUAL_P means RTTI entry;
741 We warn when RTTI is lost because non-RTTI previals; we silently
742 accept the other case. */
743 while (!end2
744 && (end1
745 || (DECL_ASSEMBLER_NAME (ref1->referred->decl)
746 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
747 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
748 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
750 if (!class_type->rtti_broken
751 && warning_at (DECL_SOURCE_LOCATION
752 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
753 OPT_Wodr,
754 "virtual table of type %qD contains RTTI "
755 "information",
756 DECL_CONTEXT (vtable->decl)))
758 inform (DECL_SOURCE_LOCATION
759 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
760 "but is prevailed by one without from other translation "
761 "unit");
762 inform (DECL_SOURCE_LOCATION
763 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
764 "RTTI will not work on this type");
765 class_type->rtti_broken = true;
767 n2++;
768 end2 = !vtable->iterate_reference (n2, ref2);
770 while (!end1
771 && (end2
772 || (DECL_ASSEMBLER_NAME (ref2->referred->decl)
773 != DECL_ASSEMBLER_NAME (ref1->referred->decl)
774 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
775 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
777 n1++;
778 end1 = !prevailing->iterate_reference (n1, ref1);
781 /* Finished? */
782 if (end1 && end2)
784 /* Extra paranoia; compare the sizes. We do not have information
785 about virtual inheritance offsets, so just be sure that these
786 match.
787 Do this as very last check so the not very informative error
788 is not output too often. */
789 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
791 class_type->odr_violated = true;
792 if (warning_at (DECL_SOURCE_LOCATION
793 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
794 OPT_Wodr,
795 "virtual table of type %qD violates "
796 "one definition rule ",
797 DECL_CONTEXT (vtable->decl)))
799 inform (DECL_SOURCE_LOCATION
800 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
801 "the conflicting type defined in another translation "
802 "unit has virtual table of different size");
805 return;
808 if (!end1 && !end2)
810 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
811 == DECL_ASSEMBLER_NAME (ref2->referred->decl))
812 continue;
814 class_type->odr_violated = true;
816 /* If the loops above stopped on non-virtual pointer, we have
817 mismatch in RTTI information mangling. */
818 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
819 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
821 if (warning_at (DECL_SOURCE_LOCATION
822 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
823 OPT_Wodr,
824 "virtual table of type %qD violates "
825 "one definition rule ",
826 DECL_CONTEXT (vtable->decl)))
828 inform (DECL_SOURCE_LOCATION
829 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
830 "the conflicting type defined in another translation "
831 "unit with different RTTI information");
833 return;
835 /* At this point both REF1 and REF2 points either to virtual table
836 or virtual method. If one points to virtual table and other to
837 method we can complain the same way as if one table was shorter
838 than other pointing out the extra method. */
839 if (TREE_CODE (ref1->referred->decl)
840 != TREE_CODE (ref2->referred->decl))
842 if (TREE_CODE (ref1->referred->decl) == VAR_DECL)
843 end1 = true;
844 else if (TREE_CODE (ref2->referred->decl) == VAR_DECL)
845 end2 = true;
849 class_type->odr_violated = true;
851 /* Complain about size mismatch. Either we have too many virutal
852 functions or too many virtual table pointers. */
853 if (end1 || end2)
855 if (end1)
857 varpool_node *tmp = prevailing;
858 prevailing = vtable;
859 vtable = tmp;
860 ref1 = ref2;
862 if (warning_at (DECL_SOURCE_LOCATION
863 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
864 OPT_Wodr,
865 "virtual table of type %qD violates "
866 "one definition rule",
867 DECL_CONTEXT (vtable->decl)))
869 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
871 inform (DECL_SOURCE_LOCATION
872 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
873 "the conflicting type defined in another translation "
874 "unit");
875 inform (DECL_SOURCE_LOCATION
876 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
877 "contains additional virtual method %qD",
878 ref1->referred->decl);
880 else
882 inform (DECL_SOURCE_LOCATION
883 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
884 "the conflicting type defined in another translation "
885 "unit has virtual table table with more entries");
888 return;
891 /* And in the last case we have either mistmatch in between two virtual
892 methods or two virtual table pointers. */
893 if (warning_at (DECL_SOURCE_LOCATION
894 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
895 "virtual table of type %qD violates "
896 "one definition rule ",
897 DECL_CONTEXT (vtable->decl)))
899 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
901 inform (DECL_SOURCE_LOCATION
902 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
903 "the conflicting type defined in another translation "
904 "unit");
905 gcc_assert (TREE_CODE (ref2->referred->decl)
906 == FUNCTION_DECL);
907 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
908 "virtual method %qD", ref1->referred->decl);
909 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
910 "ought to match virtual method %qD but does not",
911 ref2->referred->decl);
913 else
914 inform (DECL_SOURCE_LOCATION
915 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
916 "the conflicting type defined in another translation "
917 "unit has virtual table table with different contents");
918 return;
923 /* Output ODR violation warning about T1 and T2 with REASON.
924 Display location of ST1 and ST2 if REASON speaks about field or
925 method of the type.
926 If WARN is false, do nothing. Set WARNED if warning was indeed
927 output. */
929 void
930 warn_odr (tree t1, tree t2, tree st1, tree st2,
931 bool warn, bool *warned, const char *reason)
933 tree decl2 = TYPE_NAME (t2);
934 if (warned)
935 *warned = false;
937 if (!warn || !TYPE_NAME(t1))
938 return;
940 /* ODR warnings are output druing LTO streaming; we must apply location
941 cache for potential warnings to be output correctly. */
942 if (lto_location_cache::current_cache)
943 lto_location_cache::current_cache->apply_location_cache ();
945 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
946 "type %qT violates one definition rule",
947 t1))
948 return;
949 if (!st1 && !st2)
951 /* For FIELD_DECL support also case where one of fields is
952 NULL - this is used when the structures have mismatching number of
953 elements. */
954 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
956 inform (DECL_SOURCE_LOCATION (decl2),
957 "a different type is defined in another translation unit");
958 if (!st1)
960 st1 = st2;
961 st2 = NULL;
963 inform (DECL_SOURCE_LOCATION (st1),
964 "the first difference of corresponding definitions is field %qD",
965 st1);
966 if (st2)
967 decl2 = st2;
969 else if (TREE_CODE (st1) == FUNCTION_DECL)
971 inform (DECL_SOURCE_LOCATION (decl2),
972 "a different type is defined in another translation unit");
973 inform (DECL_SOURCE_LOCATION (st1),
974 "the first difference of corresponding definitions is method %qD",
975 st1);
976 decl2 = st2;
978 else
979 return;
980 inform (DECL_SOURCE_LOCATION (decl2), reason);
982 if (warned)
983 *warned = true;
986 /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
987 because they are used on same place in ODR matching types.
988 They are not; inform the user. */
990 void
991 warn_types_mismatch (tree t1, tree t2)
993 /* If types have names and they are different, it is most informative to
994 output those. */
995 if (TYPE_NAME (t1) && TYPE_NAME (t2)
996 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
997 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
998 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
999 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
1001 char *name1 = xstrdup (cplus_demangle
1002 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
1003 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1004 char *name2 = cplus_demangle
1005 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
1006 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1007 if (name1 && name2 && strcmp (name1, name2))
1009 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1010 "type name %<%s%> should match type name %<%s%>",
1011 name1, name2);
1012 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
1013 "the incompatible type is defined here");
1014 free (name1);
1015 return;
1017 free (name1);
1019 /* It is a quite common bug to reference anonymous namespace type in
1020 non-anonymous namespace class. */
1021 if (type_in_anonymous_namespace_p (t1)
1022 || type_in_anonymous_namespace_p (t2))
1024 if (!type_in_anonymous_namespace_p (t1))
1026 tree tmp = t1;;
1027 t1 = t2;
1028 t2 = tmp;
1030 if (TYPE_NAME (t1) && TYPE_NAME (t2))
1032 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1033 "type %qT defined in anonymous namespace can not match "
1034 "type %qT",
1035 t1, t2);
1036 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
1037 "the incompatible type defined in anonymous namespace in "
1038 "another translation unit");
1040 else
1041 inform (UNKNOWN_LOCATION,
1042 "types in anonymous namespace does not match across "
1043 "translation unit boundary");
1044 return;
1046 /* A tricky case are component types. Often they appear the same in source
1047 code and the mismatch is dragged in by type they are build from.
1048 Look for those differences in subtypes and try to be informative. In other
1049 cases just output nothing because the source code is probably different
1050 and in this case we already output a all necessary info. */
1051 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1053 if (TREE_CODE (t1) == TREE_CODE (t2))
1055 hash_set<type_pair,pair_traits> visited;
1056 if (TREE_CODE (t1) == ARRAY_TYPE
1057 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1059 tree i1 = TYPE_DOMAIN (t1);
1060 tree i2 = TYPE_DOMAIN (t2);
1062 if (i1 && i2
1063 && TYPE_MAX_VALUE (i1)
1064 && TYPE_MAX_VALUE (i2)
1065 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1066 TYPE_MAX_VALUE (i2), 0))
1068 inform (UNKNOWN_LOCATION,
1069 "array types have different bounds");
1070 return;
1073 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1074 && !odr_subtypes_equivalent_p (TREE_TYPE (t1),
1075 TREE_TYPE (t2),
1076 &visited))
1077 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1078 else if (TREE_CODE (t1) == METHOD_TYPE
1079 || TREE_CODE (t1) == FUNCTION_TYPE)
1081 tree parms1, parms2;
1082 int count = 1;
1084 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1085 &visited))
1087 inform (UNKNOWN_LOCATION, "return value type mismatch");
1088 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1089 return;
1091 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1092 parms1 && parms2;
1093 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1094 count++)
1096 if (!odr_subtypes_equivalent_p
1097 (TREE_VALUE (parms1), TREE_VALUE (parms2), &visited))
1099 inform (UNKNOWN_LOCATION,
1100 "type mismatch in parameter %i", count);
1101 warn_types_mismatch (TREE_VALUE (parms1),
1102 TREE_VALUE (parms2));
1103 return;
1106 if (parms1 || parms2)
1108 inform (UNKNOWN_LOCATION,
1109 "types have different parameter counts");
1110 return;
1114 return;
1116 /* This should not happen but if it does, the warning would not be helpful.
1117 TODO: turn it into assert next stage1. */
1118 if (TYPE_NAME (t1) == TYPE_NAME (t2))
1119 return;
1120 /* In Firefox it is a common bug to have same types but in
1121 different namespaces. Be a bit more informative on
1122 this. */
1123 if (TYPE_CONTEXT (t1) && TYPE_CONTEXT (t2)
1124 && (((TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL)
1125 != (TREE_CODE (TYPE_CONTEXT (t2)) == NAMESPACE_DECL))
1126 || (TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL
1127 && (DECL_NAME (TYPE_CONTEXT (t1)) !=
1128 DECL_NAME (TYPE_CONTEXT (t2))))))
1129 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1130 "type %qT should match type %qT but is defined "
1131 "in different namespace ",
1132 t1, t2);
1133 else if (types_odr_comparable (t1, t2, true)
1134 && types_same_for_odr (t1, t2, true))
1135 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1136 "type %qT should match type %qT that itself violate "
1137 "one definition rule",
1138 t1, t2);
1139 else
1140 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1141 "type %qT should match type %qT",
1142 t1, t2);
1143 if (DECL_SOURCE_LOCATION (TYPE_NAME (t2)) > BUILTINS_LOCATION)
1144 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
1145 "the incompatible type is defined here");
1148 /* Compare T1 and T2, report ODR violations if WARN is true and set
1149 WARNED to true if anything is reported. Return true if types match.
1150 If true is returned, the types are also compatible in the sense of
1151 gimple_canonical_types_compatible_p. */
1153 static bool
1154 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1155 hash_set<type_pair,pair_traits> *visited)
1157 /* Check first for the obvious case of pointer identity. */
1158 if (t1 == t2)
1159 return true;
1160 gcc_assert (!type_in_anonymous_namespace_p (t1));
1161 gcc_assert (!type_in_anonymous_namespace_p (t2));
1163 /* Can't be the same type if the types don't have the same code. */
1164 if (TREE_CODE (t1) != TREE_CODE (t2))
1166 warn_odr (t1, t2, NULL, NULL, warn, warned,
1167 G_("a different type is defined in another translation unit"));
1168 return false;
1171 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1173 warn_odr (t1, t2, NULL, NULL, warn, warned,
1174 G_("a type with different qualifiers is defined in another "
1175 "translation unit"));
1176 return false;
1179 if (comp_type_attributes (t1, t2) != 1)
1181 warn_odr (t1, t2, NULL, NULL, warn, warned,
1182 G_("a type with attributes "
1183 "is defined in another translation unit"));
1184 return false;
1187 if (TREE_CODE (t1) == ENUMERAL_TYPE
1188 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1190 tree v1, v2;
1191 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1192 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1194 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1196 warn_odr (t1, t2, NULL, NULL, warn, warned,
1197 G_("an enum with different value name"
1198 " is defined in another translation unit"));
1199 return false;
1201 if (TREE_VALUE (v1) != TREE_VALUE (v2)
1202 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
1203 DECL_INITIAL (TREE_VALUE (v2)), 0))
1205 warn_odr (t1, t2, NULL, NULL, warn, warned,
1206 G_("an enum with different values is defined"
1207 " in another translation unit"));
1208 return false;
1211 if (v1 || v2)
1213 warn_odr (t1, t2, NULL, NULL, warn, warned,
1214 G_("an enum with mismatching number of values "
1215 "is defined in another translation unit"));
1216 return false;
1220 /* Non-aggregate types can be handled cheaply. */
1221 if (INTEGRAL_TYPE_P (t1)
1222 || SCALAR_FLOAT_TYPE_P (t1)
1223 || FIXED_POINT_TYPE_P (t1)
1224 || TREE_CODE (t1) == VECTOR_TYPE
1225 || TREE_CODE (t1) == COMPLEX_TYPE
1226 || TREE_CODE (t1) == OFFSET_TYPE
1227 || POINTER_TYPE_P (t1))
1229 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1231 warn_odr (t1, t2, NULL, NULL, warn, warned,
1232 G_("a type with different precision is defined "
1233 "in another translation unit"));
1234 return false;
1236 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1238 warn_odr (t1, t2, NULL, NULL, warn, warned,
1239 G_("a type with different signedness is defined "
1240 "in another translation unit"));
1241 return false;
1244 if (TREE_CODE (t1) == INTEGER_TYPE
1245 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1247 /* char WRT uint_8? */
1248 warn_odr (t1, t2, NULL, NULL, warn, warned,
1249 G_("a different type is defined in another "
1250 "translation unit"));
1251 return false;
1254 /* For canonical type comparisons we do not want to build SCCs
1255 so we cannot compare pointed-to types. But we can, for now,
1256 require the same pointed-to type kind and match what
1257 useless_type_conversion_p would do. */
1258 if (POINTER_TYPE_P (t1))
1260 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1261 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1263 warn_odr (t1, t2, NULL, NULL, warn, warned,
1264 G_("it is defined as a pointer in different address "
1265 "space in another translation unit"));
1266 return false;
1269 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1271 warn_odr (t1, t2, NULL, NULL, warn, warned,
1272 G_("it is defined as a pointer to different type "
1273 "in another translation unit"));
1274 if (warn && warned)
1275 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1276 return false;
1280 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1281 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1283 /* Probably specific enough. */
1284 warn_odr (t1, t2, NULL, NULL, warn, warned,
1285 G_("a different type is defined "
1286 "in another translation unit"));
1287 if (warn && warned)
1288 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1289 return false;
1292 /* Do type-specific comparisons. */
1293 else switch (TREE_CODE (t1))
1295 case ARRAY_TYPE:
1297 /* Array types are the same if the element types are the same and
1298 the number of elements are the same. */
1299 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1301 warn_odr (t1, t2, NULL, NULL, warn, warned,
1302 G_("a different type is defined in another "
1303 "translation unit"));
1304 if (warn && warned)
1305 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1307 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1308 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1309 == TYPE_NONALIASED_COMPONENT (t2));
1311 tree i1 = TYPE_DOMAIN (t1);
1312 tree i2 = TYPE_DOMAIN (t2);
1314 /* For an incomplete external array, the type domain can be
1315 NULL_TREE. Check this condition also. */
1316 if (i1 == NULL_TREE || i2 == NULL_TREE)
1317 return true;
1319 tree min1 = TYPE_MIN_VALUE (i1);
1320 tree min2 = TYPE_MIN_VALUE (i2);
1321 tree max1 = TYPE_MAX_VALUE (i1);
1322 tree max2 = TYPE_MAX_VALUE (i2);
1324 /* In C++, minimums should be always 0. */
1325 gcc_assert (min1 == min2);
1326 if (!operand_equal_p (max1, max2, 0))
1328 warn_odr (t1, t2, NULL, NULL, warn, warned,
1329 G_("an array of different size is defined "
1330 "in another translation unit"));
1331 return false;
1334 break;
1336 case METHOD_TYPE:
1337 case FUNCTION_TYPE:
1338 /* Function types are the same if the return type and arguments types
1339 are the same. */
1340 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1342 warn_odr (t1, t2, NULL, NULL, warn, warned,
1343 G_("has different return value "
1344 "in another translation unit"));
1345 if (warn && warned)
1346 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1347 return false;
1350 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
1351 return true;
1352 else
1354 tree parms1, parms2;
1356 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1357 parms1 && parms2;
1358 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1360 if (!odr_subtypes_equivalent_p
1361 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited))
1363 warn_odr (t1, t2, NULL, NULL, warn, warned,
1364 G_("has different parameters in another "
1365 "translation unit"));
1366 if (warn && warned)
1367 warn_types_mismatch (TREE_VALUE (parms1),
1368 TREE_VALUE (parms2));
1369 return false;
1373 if (parms1 || parms2)
1375 warn_odr (t1, t2, NULL, NULL, warn, warned,
1376 G_("has different parameters "
1377 "in another translation unit"));
1378 return false;
1381 return true;
1384 case RECORD_TYPE:
1385 case UNION_TYPE:
1386 case QUAL_UNION_TYPE:
1388 tree f1, f2;
1390 /* For aggregate types, all the fields must be the same. */
1391 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1393 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1394 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1395 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1397 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1398 warn_odr (t1, t2, NULL, NULL, warn, warned,
1399 G_("a type defined in another translation unit "
1400 "is not polymorphic"));
1401 else
1402 warn_odr (t1, t2, NULL, NULL, warn, warned,
1403 G_("a type defined in another translation unit "
1404 "is polymorphic"));
1405 return false;
1407 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1408 f1 || f2;
1409 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1411 /* Skip non-fields. */
1412 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1413 f1 = TREE_CHAIN (f1);
1414 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1415 f2 = TREE_CHAIN (f2);
1416 if (!f1 || !f2)
1417 break;
1418 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1420 warn_odr (t1, t2, NULL, NULL, warn, warned,
1421 G_("a type with different virtual table pointers"
1422 " is defined in another translation unit"));
1423 return false;
1425 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1427 warn_odr (t1, t2, NULL, NULL, warn, warned,
1428 G_("a type with different bases is defined "
1429 "in another translation unit"));
1430 return false;
1432 if (DECL_NAME (f1) != DECL_NAME (f2)
1433 && !DECL_ARTIFICIAL (f1))
1435 warn_odr (t1, t2, f1, f2, warn, warned,
1436 G_("a field with different name is defined "
1437 "in another translation unit"));
1438 return false;
1440 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1441 TREE_TYPE (f2), visited))
1443 /* Do not warn about artificial fields and just go into
1444 generic field mismatch warning. */
1445 if (DECL_ARTIFICIAL (f1))
1446 break;
1448 warn_odr (t1, t2, f1, f2, warn, warned,
1449 G_("a field of same name but different type "
1450 "is defined in another translation unit"));
1451 if (warn && warned)
1452 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2));
1453 return false;
1455 if (!gimple_compare_field_offset (f1, f2))
1457 /* Do not warn about artificial fields and just go into
1458 generic field mismatch warning. */
1459 if (DECL_ARTIFICIAL (f1))
1460 break;
1461 warn_odr (t1, t2, f1, f2, warn, warned,
1462 G_("fields has different layout "
1463 "in another translation unit"));
1464 return false;
1466 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1467 == DECL_NONADDRESSABLE_P (f2));
1470 /* If one aggregate has more fields than the other, they
1471 are not the same. */
1472 if (f1 || f2)
1474 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1475 warn_odr (t1, t2, NULL, NULL, warn, warned,
1476 G_("a type with different virtual table pointers"
1477 " is defined in another translation unit"));
1478 else if ((f1 && DECL_ARTIFICIAL (f1))
1479 || (f2 && DECL_ARTIFICIAL (f2)))
1480 warn_odr (t1, t2, NULL, NULL, warn, warned,
1481 G_("a type with different bases is defined "
1482 "in another translation unit"));
1483 else
1484 warn_odr (t1, t2, f1, f2, warn, warned,
1485 G_("a type with different number of fields "
1486 "is defined in another translation unit"));
1488 return false;
1490 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
1491 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
1492 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
1494 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
1495 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
1496 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
1498 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
1500 warn_odr (t1, t2, f1, f2, warn, warned,
1501 G_("a different method of same type "
1502 "is defined in another translation unit"));
1503 return false;
1505 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1507 warn_odr (t1, t2, f1, f2, warn, warned,
1508 G_("s definition that differs by virtual "
1509 "keyword in another translation unit"));
1510 return false;
1512 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1514 warn_odr (t1, t2, f1, f2, warn, warned,
1515 G_("virtual table layout differs in another "
1516 "translation unit"));
1517 return false;
1519 if (odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
1521 warn_odr (t1, t2, f1, f2, warn, warned,
1522 G_("method with incompatible type is defined "
1523 "in another translation unit"));
1524 return false;
1527 if (f1 || f2)
1529 warn_odr (t1, t2, NULL, NULL, warn, warned,
1530 G_("a type with different number of methods "
1531 "is defined in another translation unit"));
1532 return false;
1536 break;
1538 case VOID_TYPE:
1539 break;
1541 default:
1542 debug_tree (t1);
1543 gcc_unreachable ();
1546 /* Those are better to come last as they are utterly uninformative. */
1547 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1548 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1550 warn_odr (t1, t2, NULL, NULL, warn, warned,
1551 G_("a type with different size "
1552 "is defined in another translation unit"));
1553 return false;
1555 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1556 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1558 warn_odr (t1, t2, NULL, NULL, warn, warned,
1559 G_("a type with different alignment "
1560 "is defined in another translation unit"));
1561 return false;
1563 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1564 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1565 TYPE_SIZE_UNIT (t2), 0));
1566 return true;
1569 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1570 from VAL->type. This may happen in LTO where tree merging did not merge
1571 all variants of the same type or due to ODR violation.
1573 Analyze and report ODR violations and add type to duplicate list.
1574 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1575 this is first time we see definition of a class return true so the
1576 base types are analyzed. */
1578 static bool
1579 add_type_duplicate (odr_type val, tree type)
1581 bool build_bases = false;
1582 bool prevail = false;
1583 bool odr_must_violate = false;
1585 if (!val->types_set)
1586 val->types_set = new hash_set<tree>;
1588 /* Chose polymorphic type as leader (this happens only in case of ODR
1589 violations. */
1590 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1591 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1592 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1593 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1595 prevail = true;
1596 build_bases = true;
1598 /* Always prefer complete type to be the leader. */
1599 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1601 prevail = true;
1602 build_bases = TYPE_BINFO (type);
1604 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1606 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1607 && TREE_CODE (type) == ENUMERAL_TYPE
1608 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1609 prevail = true;
1610 else if (TREE_CODE (val->type) == RECORD_TYPE
1611 && TREE_CODE (type) == RECORD_TYPE
1612 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1614 gcc_assert (!val->bases.length ());
1615 build_bases = true;
1616 prevail = true;
1619 if (prevail)
1621 tree tmp = type;
1623 type = val->type;
1624 val->type = tmp;
1627 val->types_set->add (type);
1629 /* If we now have a mangled name, be sure to record it to val->type
1630 so ODR hash can work. */
1632 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1633 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1634 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1636 bool merge = true;
1637 bool base_mismatch = false;
1638 unsigned int i;
1639 bool warned = false;
1640 hash_set<type_pair,pair_traits> visited;
1642 gcc_assert (in_lto_p);
1643 vec_safe_push (val->types, type);
1645 /* If both are class types, compare the bases. */
1646 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1647 && TREE_CODE (val->type) == RECORD_TYPE
1648 && TREE_CODE (type) == RECORD_TYPE
1649 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1651 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1652 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1654 if (!flag_ltrans && !warned && !val->odr_violated)
1656 tree extra_base;
1657 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1658 "a type with the same name but different "
1659 "number of polymorphic bases is "
1660 "defined in another translation unit");
1661 if (warned)
1663 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1664 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1665 extra_base = BINFO_BASE_BINFO
1666 (TYPE_BINFO (type),
1667 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1668 else
1669 extra_base = BINFO_BASE_BINFO
1670 (TYPE_BINFO (val->type),
1671 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1672 tree extra_base_type = BINFO_TYPE (extra_base);
1673 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1674 "the extra base is defined here");
1677 base_mismatch = true;
1679 else
1680 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1682 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1683 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1684 tree type1 = BINFO_TYPE (base1);
1685 tree type2 = BINFO_TYPE (base2);
1687 if (types_odr_comparable (type1, type2))
1689 if (!types_same_for_odr (type1, type2))
1690 base_mismatch = true;
1692 else
1694 hash_set<type_pair,pair_traits> visited;
1695 if (!odr_types_equivalent_p (type1, type2, false, NULL,
1696 &visited))
1697 base_mismatch = true;
1699 if (base_mismatch)
1701 if (!warned && !val->odr_violated)
1703 warn_odr (type, val->type, NULL, NULL,
1704 !warned, &warned,
1705 "a type with the same name but different base "
1706 "type is defined in another translation unit");
1707 if (warned)
1708 warn_types_mismatch (type1, type2);
1710 break;
1712 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1714 base_mismatch = true;
1715 if (!warned && !val->odr_violated)
1716 warn_odr (type, val->type, NULL, NULL,
1717 !warned, &warned,
1718 "a type with the same name but different base "
1719 "layout is defined in another translation unit");
1720 break;
1722 /* One of bases is not of complete type. */
1723 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1725 /* If we have a polymorphic type info specified for TYPE1
1726 but not for TYPE2 we possibly missed a base when recording
1727 VAL->type earlier.
1728 Be sure this does not happen. */
1729 if (TYPE_BINFO (type1)
1730 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1731 && !build_bases)
1732 odr_must_violate = true;
1733 break;
1735 /* One base is polymorphic and the other not.
1736 This ought to be diagnosed earlier, but do not ICE in the
1737 checking bellow. */
1738 else if (TYPE_BINFO (type1)
1739 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1740 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1742 if (!warned && !val->odr_violated)
1743 warn_odr (type, val->type, NULL, NULL,
1744 !warned, &warned,
1745 "a base of the type is polymorphic only in one "
1746 "translation unit");
1747 base_mismatch = true;
1748 break;
1751 if (base_mismatch)
1753 merge = false;
1754 odr_violation_reported = true;
1755 val->odr_violated = true;
1757 if (symtab->dump_file)
1759 fprintf (symtab->dump_file, "ODR base violation\n");
1761 print_node (symtab->dump_file, "", val->type, 0);
1762 putc ('\n',symtab->dump_file);
1763 print_node (symtab->dump_file, "", type, 0);
1764 putc ('\n',symtab->dump_file);
1769 /* Next compare memory layout. */
1770 if (!odr_types_equivalent_p (val->type, type,
1771 !flag_ltrans && !val->odr_violated && !warned,
1772 &warned, &visited))
1774 merge = false;
1775 odr_violation_reported = true;
1776 val->odr_violated = true;
1777 if (symtab->dump_file)
1779 fprintf (symtab->dump_file, "ODR violation\n");
1781 print_node (symtab->dump_file, "", val->type, 0);
1782 putc ('\n',symtab->dump_file);
1783 print_node (symtab->dump_file, "", type, 0);
1784 putc ('\n',symtab->dump_file);
1787 gcc_assert (val->odr_violated || !odr_must_violate);
1788 /* Sanity check that all bases will be build same way again. */
1789 #ifdef ENABLE_CHECKING
1790 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1791 && TREE_CODE (val->type) == RECORD_TYPE
1792 && TREE_CODE (type) == RECORD_TYPE
1793 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1794 && !val->odr_violated
1795 && !base_mismatch && val->bases.length ())
1797 unsigned int num_poly_bases = 0;
1798 unsigned int j;
1800 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1801 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1802 (TYPE_BINFO (type), i)))
1803 num_poly_bases++;
1804 gcc_assert (num_poly_bases == val->bases.length ());
1805 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
1806 i++)
1807 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1808 (TYPE_BINFO (type), i)))
1810 odr_type base = get_odr_type
1811 (BINFO_TYPE
1812 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1813 i)),
1814 true);
1815 gcc_assert (val->bases[j] == base);
1816 j++;
1819 #endif
1822 /* Regularize things a little. During LTO same types may come with
1823 different BINFOs. Either because their virtual table was
1824 not merged by tree merging and only later at decl merging or
1825 because one type comes with external vtable, while other
1826 with internal. We want to merge equivalent binfos to conserve
1827 memory and streaming overhead.
1829 The external vtables are more harmful: they contain references
1830 to external declarations of methods that may be defined in the
1831 merged LTO unit. For this reason we absolutely need to remove
1832 them and replace by internal variants. Not doing so will lead
1833 to incomplete answers from possible_polymorphic_call_targets.
1835 FIXME: disable for now; because ODR types are now build during
1836 streaming in, the variants do not need to be linked to the type,
1837 yet. We need to do the merging in cleanup pass to be implemented
1838 soon. */
1839 if (!flag_ltrans && merge
1840 && 0
1841 && TREE_CODE (val->type) == RECORD_TYPE
1842 && TREE_CODE (type) == RECORD_TYPE
1843 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1844 && TYPE_MAIN_VARIANT (type) == type
1845 && TYPE_MAIN_VARIANT (val->type) == val->type
1846 && BINFO_VTABLE (TYPE_BINFO (val->type))
1847 && BINFO_VTABLE (TYPE_BINFO (type)))
1849 tree master_binfo = TYPE_BINFO (val->type);
1850 tree v1 = BINFO_VTABLE (master_binfo);
1851 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1853 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1855 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1856 && operand_equal_p (TREE_OPERAND (v1, 1),
1857 TREE_OPERAND (v2, 1), 0));
1858 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1859 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1861 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1862 == DECL_ASSEMBLER_NAME (v2));
1864 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1866 unsigned int i;
1868 set_type_binfo (val->type, TYPE_BINFO (type));
1869 for (i = 0; i < val->types->length (); i++)
1871 if (TYPE_BINFO ((*val->types)[i])
1872 == master_binfo)
1873 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1875 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1877 else
1878 set_type_binfo (type, master_binfo);
1880 return build_bases;
1883 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1884 possibly new entry. */
1886 odr_type
1887 get_odr_type (tree type, bool insert)
1889 odr_type_d **slot = NULL;
1890 odr_type_d **vtable_slot = NULL;
1891 odr_type val = NULL;
1892 hashval_t hash;
1893 bool build_bases = false;
1894 bool insert_to_odr_array = false;
1895 int base_id = -1;
1897 type = main_odr_variant (type);
1899 gcc_checking_assert (can_be_name_hashed_p (type)
1900 || can_be_vtable_hashed_p (type));
1902 /* Lookup entry, first try name hash, fallback to vtable hash. */
1903 if (can_be_name_hashed_p (type))
1905 hash = hash_odr_name (type);
1906 slot = odr_hash->find_slot_with_hash (type, hash,
1907 insert ? INSERT : NO_INSERT);
1909 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
1911 hash = hash_odr_vtable (type);
1912 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
1913 insert ? INSERT : NO_INSERT);
1916 if (!slot && !vtable_slot)
1917 return NULL;
1919 /* See if we already have entry for type. */
1920 if ((slot && *slot) || (vtable_slot && *vtable_slot))
1922 if (slot && *slot)
1924 val = *slot;
1925 #ifdef ENABLE_CHECKING
1926 if (in_lto_p && can_be_vtable_hashed_p (type))
1928 hash = hash_odr_vtable (type);
1929 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
1930 NO_INSERT);
1931 gcc_assert (!vtable_slot || *vtable_slot == *slot);
1932 vtable_slot = NULL;
1934 #endif
1936 else if (*vtable_slot)
1937 val = *vtable_slot;
1939 if (val->type != type
1940 && (!val->types_set || !val->types_set->add (type)))
1942 gcc_assert (insert);
1943 /* We have type duplicate, but it may introduce vtable name or
1944 mangled name; be sure to keep hashes in sync. */
1945 if (in_lto_p && can_be_vtable_hashed_p (type)
1946 && (!vtable_slot || !*vtable_slot))
1948 if (!vtable_slot)
1950 hash = hash_odr_vtable (type);
1951 vtable_slot = odr_vtable_hash->find_slot_with_hash
1952 (type, hash, INSERT);
1953 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
1955 *vtable_slot = val;
1957 if (slot && !*slot)
1958 *slot = val;
1959 build_bases = add_type_duplicate (val, type);
1962 else
1964 val = ggc_cleared_alloc<odr_type_d> ();
1965 val->type = type;
1966 val->bases = vNULL;
1967 val->derived_types = vNULL;
1968 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
1969 build_bases = COMPLETE_TYPE_P (val->type);
1970 insert_to_odr_array = true;
1971 if (slot)
1972 *slot = val;
1973 if (vtable_slot)
1974 *vtable_slot = val;
1977 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1978 && type == TYPE_MAIN_VARIANT (type))
1980 tree binfo = TYPE_BINFO (type);
1981 unsigned int i;
1983 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
1985 val->all_derivations_known = type_all_derivations_known_p (type);
1986 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
1987 /* For now record only polymorphic types. other are
1988 pointless for devirtualization and we can not precisely
1989 determine ODR equivalency of these during LTO. */
1990 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
1992 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
1993 odr_type base = get_odr_type (base_type, true);
1994 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
1995 base->derived_types.safe_push (val);
1996 val->bases.safe_push (base);
1997 if (base->id > base_id)
1998 base_id = base->id;
2001 /* Ensure that type always appears after bases. */
2002 if (insert_to_odr_array)
2004 if (odr_types_ptr)
2005 val->id = odr_types.length ();
2006 vec_safe_push (odr_types_ptr, val);
2008 else if (base_id > val->id)
2010 odr_types[val->id] = 0;
2011 /* Be sure we did not recorded any derived types; these may need
2012 renumbering too. */
2013 gcc_assert (val->derived_types.length() == 0);
2014 if (odr_types_ptr)
2015 val->id = odr_types.length ();
2016 vec_safe_push (odr_types_ptr, val);
2018 return val;
2021 /* Add TYPE od ODR type hash. */
2023 void
2024 register_odr_type (tree type)
2026 if (!odr_hash)
2028 odr_hash = new odr_hash_type (23);
2029 if (in_lto_p)
2030 odr_vtable_hash = new odr_vtable_hash_type (23);
2032 /* Arrange things to be nicer and insert main variants first. */
2033 if (odr_type_p (TYPE_MAIN_VARIANT (type)))
2034 get_odr_type (TYPE_MAIN_VARIANT (type), true);
2035 if (TYPE_MAIN_VARIANT (type) != type)
2036 get_odr_type (type, true);
2039 /* Return true if type is known to have no derivations. */
2041 bool
2042 type_known_to_have_no_deriavations_p (tree t)
2044 return (type_all_derivations_known_p (t)
2045 && (TYPE_FINAL_P (t)
2046 || (odr_hash
2047 && !get_odr_type (t, true)->derived_types.length())));
2050 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2051 recursive printing. */
2053 static void
2054 dump_odr_type (FILE *f, odr_type t, int indent=0)
2056 unsigned int i;
2057 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2058 print_generic_expr (f, t->type, TDF_SLIM);
2059 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2060 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2061 if (TYPE_NAME (t->type))
2063 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2064 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2065 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2066 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2067 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2068 IDENTIFIER_POINTER
2069 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2071 if (t->bases.length ())
2073 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2074 for (i = 0; i < t->bases.length (); i++)
2075 fprintf (f, " %i", t->bases[i]->id);
2076 fprintf (f, "\n");
2078 if (t->derived_types.length ())
2080 fprintf (f, "%*s derived types:\n", indent * 2, "");
2081 for (i = 0; i < t->derived_types.length (); i++)
2082 dump_odr_type (f, t->derived_types[i], indent + 1);
2084 fprintf (f, "\n");
2087 /* Dump the type inheritance graph. */
2089 static void
2090 dump_type_inheritance_graph (FILE *f)
2092 unsigned int i;
2093 if (!odr_types_ptr)
2094 return;
2095 fprintf (f, "\n\nType inheritance graph:\n");
2096 for (i = 0; i < odr_types.length (); i++)
2098 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2099 dump_odr_type (f, odr_types[i]);
2101 for (i = 0; i < odr_types.length (); i++)
2103 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
2105 unsigned int j;
2106 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2107 print_node (f, "", odr_types[i]->type, 0);
2108 for (j = 0; j < odr_types[i]->types->length (); j++)
2110 tree t;
2111 fprintf (f, "duplicate #%i\n", j);
2112 print_node (f, "", (*odr_types[i]->types)[j], 0);
2113 t = (*odr_types[i]->types)[j];
2114 while (TYPE_P (t) && TYPE_CONTEXT (t))
2116 t = TYPE_CONTEXT (t);
2117 print_node (f, "", t, 0);
2119 putc ('\n',f);
2125 /* Given method type T, return type of class it belongs to.
2126 Look up this pointer and get its type. */
2128 tree
2129 method_class_type (const_tree t)
2131 tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
2132 gcc_assert (TREE_CODE (t) == METHOD_TYPE);
2134 return TREE_TYPE (first_parm_type);
2137 /* Initialize IPA devirt and build inheritance tree graph. */
2139 void
2140 build_type_inheritance_graph (void)
2142 struct symtab_node *n;
2143 FILE *inheritance_dump_file;
2144 int flags;
2146 if (odr_hash)
2147 return;
2148 timevar_push (TV_IPA_INHERITANCE);
2149 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2150 odr_hash = new odr_hash_type (23);
2151 if (in_lto_p)
2152 odr_vtable_hash = new odr_vtable_hash_type (23);
2154 /* We reconstruct the graph starting of types of all methods seen in the
2155 the unit. */
2156 FOR_EACH_SYMBOL (n)
2157 if (is_a <cgraph_node *> (n)
2158 && DECL_VIRTUAL_P (n->decl)
2159 && n->real_symbol_p ())
2160 get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n->decl))),
2161 true);
2163 /* Look also for virtual tables of types that do not define any methods.
2165 We need it in a case where class B has virtual base of class A
2166 re-defining its virtual method and there is class C with no virtual
2167 methods with B as virtual base.
2169 Here we output B's virtual method in two variant - for non-virtual
2170 and virtual inheritance. B's virtual table has non-virtual version,
2171 while C's has virtual.
2173 For this reason we need to know about C in order to include both
2174 variants of B. More correctly, record_target_from_binfo should
2175 add both variants of the method when walking B, but we have no
2176 link in between them.
2178 We rely on fact that either the method is exported and thus we
2179 assume it is called externally or C is in anonymous namespace and
2180 thus we will see the vtable. */
2182 else if (is_a <varpool_node *> (n)
2183 && DECL_VIRTUAL_P (n->decl)
2184 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2185 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2186 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2187 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2188 if (inheritance_dump_file)
2190 dump_type_inheritance_graph (inheritance_dump_file);
2191 dump_end (TDI_inheritance, inheritance_dump_file);
2193 timevar_pop (TV_IPA_INHERITANCE);
2196 /* Return true if N has reference from live virtual table
2197 (and thus can be a destination of polymorphic call).
2198 Be conservatively correct when callgraph is not built or
2199 if the method may be referred externally. */
2201 static bool
2202 referenced_from_vtable_p (struct cgraph_node *node)
2204 int i;
2205 struct ipa_ref *ref;
2206 bool found = false;
2208 if (node->externally_visible
2209 || DECL_EXTERNAL (node->decl)
2210 || node->used_from_other_partition)
2211 return true;
2213 /* Keep this test constant time.
2214 It is unlikely this can happen except for the case where speculative
2215 devirtualization introduced many speculative edges to this node.
2216 In this case the target is very likely alive anyway. */
2217 if (node->ref_list.referring.length () > 100)
2218 return true;
2220 /* We need references built. */
2221 if (symtab->state <= CONSTRUCTION)
2222 return true;
2224 for (i = 0; node->iterate_referring (i, ref); i++)
2225 if ((ref->use == IPA_REF_ALIAS
2226 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2227 || (ref->use == IPA_REF_ADDR
2228 && TREE_CODE (ref->referring->decl) == VAR_DECL
2229 && DECL_VIRTUAL_P (ref->referring->decl)))
2231 found = true;
2232 break;
2234 return found;
2237 /* If TARGET has associated node, record it in the NODES array.
2238 CAN_REFER specify if program can refer to the target directly.
2239 if TARGET is unknown (NULL) or it can not be inserted (for example because
2240 its body was already removed and there is no way to refer to it), clear
2241 COMPLETEP. */
2243 static void
2244 maybe_record_node (vec <cgraph_node *> &nodes,
2245 tree target, hash_set<tree> *inserted,
2246 bool can_refer,
2247 bool *completep)
2249 struct cgraph_node *target_node, *alias_target;
2250 enum availability avail;
2252 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
2253 list of targets; the runtime effect of calling them is undefined.
2254 Only "real" virtual methods should be accounted. */
2255 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
2256 return;
2258 if (!can_refer)
2260 /* The only case when method of anonymous namespace becomes unreferable
2261 is when we completely optimized it out. */
2262 if (flag_ltrans
2263 || !target
2264 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2265 *completep = false;
2266 return;
2269 if (!target)
2270 return;
2272 target_node = cgraph_node::get (target);
2274 /* Prefer alias target over aliases, so we do not get confused by
2275 fake duplicates. */
2276 if (target_node)
2278 alias_target = target_node->ultimate_alias_target (&avail);
2279 if (target_node != alias_target
2280 && avail >= AVAIL_AVAILABLE
2281 && target_node->get_availability ())
2282 target_node = alias_target;
2285 /* Method can only be called by polymorphic call if any
2286 of vtables referring to it are alive.
2288 While this holds for non-anonymous functions, too, there are
2289 cases where we want to keep them in the list; for example
2290 inline functions with -fno-weak are static, but we still
2291 may devirtualize them when instance comes from other unit.
2292 The same holds for LTO.
2294 Currently we ignore these functions in speculative devirtualization.
2295 ??? Maybe it would make sense to be more aggressive for LTO even
2296 elsewhere. */
2297 if (!flag_ltrans
2298 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2299 && (!target_node
2300 || !referenced_from_vtable_p (target_node)))
2302 /* See if TARGET is useful function we can deal with. */
2303 else if (target_node != NULL
2304 && (TREE_PUBLIC (target)
2305 || DECL_EXTERNAL (target)
2306 || target_node->definition)
2307 && target_node->real_symbol_p ())
2309 gcc_assert (!target_node->global.inlined_to);
2310 gcc_assert (target_node->real_symbol_p ());
2311 if (!inserted->add (target))
2313 cached_polymorphic_call_targets->add (target_node);
2314 nodes.safe_push (target_node);
2317 else if (completep
2318 && (!type_in_anonymous_namespace_p
2319 (DECL_CONTEXT (target))
2320 || flag_ltrans))
2321 *completep = false;
2324 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2325 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2326 method in vtable and insert method to NODES array
2327 or BASES_TO_CONSIDER if this array is non-NULL.
2328 Otherwise recurse to base BINFOs.
2329 This matches what get_binfo_at_offset does, but with offset
2330 being unknown.
2332 TYPE_BINFOS is a stack of BINFOS of types with defined
2333 virtual table seen on way from class type to BINFO.
2335 MATCHED_VTABLES tracks virtual tables we already did lookup
2336 for virtual function in. INSERTED tracks nodes we already
2337 inserted.
2339 ANONYMOUS is true if BINFO is part of anonymous namespace.
2341 Clear COMPLETEP when we hit unreferable target.
2344 static void
2345 record_target_from_binfo (vec <cgraph_node *> &nodes,
2346 vec <tree> *bases_to_consider,
2347 tree binfo,
2348 tree otr_type,
2349 vec <tree> &type_binfos,
2350 HOST_WIDE_INT otr_token,
2351 tree outer_type,
2352 HOST_WIDE_INT offset,
2353 hash_set<tree> *inserted,
2354 hash_set<tree> *matched_vtables,
2355 bool anonymous,
2356 bool *completep)
2358 tree type = BINFO_TYPE (binfo);
2359 int i;
2360 tree base_binfo;
2363 if (BINFO_VTABLE (binfo))
2364 type_binfos.safe_push (binfo);
2365 if (types_same_for_odr (type, outer_type))
2367 int i;
2368 tree type_binfo = NULL;
2370 /* Look up BINFO with virtual table. For normal types it is always last
2371 binfo on stack. */
2372 for (i = type_binfos.length () - 1; i >= 0; i--)
2373 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2375 type_binfo = type_binfos[i];
2376 break;
2378 if (BINFO_VTABLE (binfo))
2379 type_binfos.pop ();
2380 /* If this is duplicated BINFO for base shared by virtual inheritance,
2381 we may not have its associated vtable. This is not a problem, since
2382 we will walk it on the other path. */
2383 if (!type_binfo)
2384 return;
2385 tree inner_binfo = get_binfo_at_offset (type_binfo,
2386 offset, otr_type);
2387 if (!inner_binfo)
2389 gcc_assert (odr_violation_reported);
2390 return;
2392 /* For types in anonymous namespace first check if the respective vtable
2393 is alive. If not, we know the type can't be called. */
2394 if (!flag_ltrans && anonymous)
2396 tree vtable = BINFO_VTABLE (inner_binfo);
2397 varpool_node *vnode;
2399 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2400 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2401 vnode = varpool_node::get (vtable);
2402 if (!vnode || !vnode->definition)
2403 return;
2405 gcc_assert (inner_binfo);
2406 if (bases_to_consider
2407 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2408 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2410 bool can_refer;
2411 tree target = gimple_get_virt_method_for_binfo (otr_token,
2412 inner_binfo,
2413 &can_refer);
2414 if (!bases_to_consider)
2415 maybe_record_node (nodes, target, inserted, can_refer, completep);
2416 /* Destructors are never called via construction vtables. */
2417 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2418 bases_to_consider->safe_push (target);
2420 return;
2423 /* Walk bases. */
2424 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2425 /* Walking bases that have no virtual method is pointless exercise. */
2426 if (polymorphic_type_binfo_p (base_binfo))
2427 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2428 type_binfos,
2429 otr_token, outer_type, offset, inserted,
2430 matched_vtables, anonymous, completep);
2431 if (BINFO_VTABLE (binfo))
2432 type_binfos.pop ();
2435 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2436 of TYPE, insert them to NODES, recurse into derived nodes.
2437 INSERTED is used to avoid duplicate insertions of methods into NODES.
2438 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2439 Clear COMPLETEP if unreferable target is found.
2441 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2442 all cases where BASE_SKIPPED is true (because the base is abstract
2443 class). */
2445 static void
2446 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2447 hash_set<tree> *inserted,
2448 hash_set<tree> *matched_vtables,
2449 tree otr_type,
2450 odr_type type,
2451 HOST_WIDE_INT otr_token,
2452 tree outer_type,
2453 HOST_WIDE_INT offset,
2454 bool *completep,
2455 vec <tree> &bases_to_consider,
2456 bool consider_construction)
2458 tree binfo = TYPE_BINFO (type->type);
2459 unsigned int i;
2460 auto_vec <tree, 8> type_binfos;
2461 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2463 /* We may need to consider types w/o instances because of possible derived
2464 types using their methods either directly or via construction vtables.
2465 We are safe to skip them when all derivations are known, since we will
2466 handle them later.
2467 This is done by recording them to BASES_TO_CONSIDER array. */
2468 if (possibly_instantiated || consider_construction)
2470 record_target_from_binfo (nodes,
2471 (!possibly_instantiated
2472 && type_all_derivations_known_p (type->type))
2473 ? &bases_to_consider : NULL,
2474 binfo, otr_type, type_binfos, otr_token,
2475 outer_type, offset,
2476 inserted, matched_vtables,
2477 type->anonymous_namespace, completep);
2479 for (i = 0; i < type->derived_types.length (); i++)
2480 possible_polymorphic_call_targets_1 (nodes, inserted,
2481 matched_vtables,
2482 otr_type,
2483 type->derived_types[i],
2484 otr_token, outer_type, offset, completep,
2485 bases_to_consider, consider_construction);
2488 /* Cache of queries for polymorphic call targets.
2490 Enumerating all call targets may get expensive when there are many
2491 polymorphic calls in the program, so we memoize all the previous
2492 queries and avoid duplicated work. */
2494 struct polymorphic_call_target_d
2496 HOST_WIDE_INT otr_token;
2497 ipa_polymorphic_call_context context;
2498 odr_type type;
2499 vec <cgraph_node *> targets;
2500 tree decl_warning;
2501 int type_warning;
2502 bool complete;
2503 bool speculative;
2506 /* Polymorphic call target cache helpers. */
2508 struct polymorphic_call_target_hasher
2510 typedef polymorphic_call_target_d *value_type;
2511 typedef polymorphic_call_target_d *compare_type;
2512 static inline hashval_t hash (const polymorphic_call_target_d *);
2513 static inline bool equal (const polymorphic_call_target_d *,
2514 const polymorphic_call_target_d *);
2515 static inline void remove (polymorphic_call_target_d *);
2518 /* Return the computed hashcode for ODR_QUERY. */
2520 inline hashval_t
2521 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2523 inchash::hash hstate (odr_query->otr_token);
2525 hstate.add_wide_int (odr_query->type->id);
2526 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2527 hstate.add_wide_int (odr_query->context.offset);
2529 if (odr_query->context.speculative_outer_type)
2531 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2532 hstate.add_wide_int (odr_query->context.speculative_offset);
2534 hstate.add_flag (odr_query->speculative);
2535 hstate.add_flag (odr_query->context.maybe_in_construction);
2536 hstate.add_flag (odr_query->context.maybe_derived_type);
2537 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2538 hstate.commit_flag ();
2539 return hstate.end ();
2542 /* Compare cache entries T1 and T2. */
2544 inline bool
2545 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2546 const polymorphic_call_target_d *t2)
2548 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2549 && t1->speculative == t2->speculative
2550 && t1->context.offset == t2->context.offset
2551 && t1->context.speculative_offset == t2->context.speculative_offset
2552 && t1->context.outer_type == t2->context.outer_type
2553 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2554 && t1->context.maybe_in_construction
2555 == t2->context.maybe_in_construction
2556 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2557 && (t1->context.speculative_maybe_derived_type
2558 == t2->context.speculative_maybe_derived_type));
2561 /* Remove entry in polymorphic call target cache hash. */
2563 inline void
2564 polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2566 v->targets.release ();
2567 free (v);
2570 /* Polymorphic call target query cache. */
2572 typedef hash_table<polymorphic_call_target_hasher>
2573 polymorphic_call_target_hash_type;
2574 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2576 /* Destroy polymorphic call target query cache. */
2578 static void
2579 free_polymorphic_call_targets_hash ()
2581 if (cached_polymorphic_call_targets)
2583 delete polymorphic_call_target_hash;
2584 polymorphic_call_target_hash = NULL;
2585 delete cached_polymorphic_call_targets;
2586 cached_polymorphic_call_targets = NULL;
2590 /* When virtual function is removed, we may need to flush the cache. */
2592 static void
2593 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2595 if (cached_polymorphic_call_targets
2596 && cached_polymorphic_call_targets->contains (n))
2597 free_polymorphic_call_targets_hash ();
2600 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2602 tree
2603 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2604 tree vtable)
2606 tree v = BINFO_VTABLE (binfo);
2607 int i;
2608 tree base_binfo;
2609 unsigned HOST_WIDE_INT this_offset;
2611 if (v)
2613 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2614 gcc_unreachable ();
2616 if (offset == this_offset
2617 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2618 return binfo;
2621 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2622 if (polymorphic_type_binfo_p (base_binfo))
2624 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2625 if (base_binfo)
2626 return base_binfo;
2628 return NULL;
2631 /* T is known constant value of virtual table pointer.
2632 Store virtual table to V and its offset to OFFSET.
2633 Return false if T does not look like virtual table reference. */
2635 bool
2636 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2637 unsigned HOST_WIDE_INT *offset)
2639 /* We expect &MEM[(void *)&virtual_table + 16B].
2640 We obtain object's BINFO from the context of the virtual table.
2641 This one contains pointer to virtual table represented via
2642 POINTER_PLUS_EXPR. Verify that this pointer matches what
2643 we propagated through.
2645 In the case of virtual inheritance, the virtual tables may
2646 be nested, i.e. the offset may be different from 16 and we may
2647 need to dive into the type representation. */
2648 if (TREE_CODE (t) == ADDR_EXPR
2649 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2650 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2651 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2652 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2653 == VAR_DECL)
2654 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2655 (TREE_OPERAND (t, 0), 0), 0)))
2657 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2658 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2659 return true;
2662 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2663 We need to handle it when T comes from static variable initializer or
2664 BINFO. */
2665 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2667 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2668 t = TREE_OPERAND (t, 0);
2670 else
2671 *offset = 0;
2673 if (TREE_CODE (t) != ADDR_EXPR)
2674 return false;
2675 *v = TREE_OPERAND (t, 0);
2676 return true;
2679 /* T is known constant value of virtual table pointer. Return BINFO of the
2680 instance type. */
2682 tree
2683 vtable_pointer_value_to_binfo (const_tree t)
2685 tree vtable;
2686 unsigned HOST_WIDE_INT offset;
2688 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2689 return NULL_TREE;
2691 /* FIXME: for stores of construction vtables we return NULL,
2692 because we do not have BINFO for those. Eventually we should fix
2693 our representation to allow this case to be handled, too.
2694 In the case we see store of BINFO we however may assume
2695 that standard folding will be able to cope with it. */
2696 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2697 offset, vtable);
2700 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2701 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2702 and insert them in NODES.
2704 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2706 static void
2707 record_targets_from_bases (tree otr_type,
2708 HOST_WIDE_INT otr_token,
2709 tree outer_type,
2710 HOST_WIDE_INT offset,
2711 vec <cgraph_node *> &nodes,
2712 hash_set<tree> *inserted,
2713 hash_set<tree> *matched_vtables,
2714 bool *completep)
2716 while (true)
2718 HOST_WIDE_INT pos, size;
2719 tree base_binfo;
2720 tree fld;
2722 if (types_same_for_odr (outer_type, otr_type))
2723 return;
2725 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2727 if (TREE_CODE (fld) != FIELD_DECL)
2728 continue;
2730 pos = int_bit_position (fld);
2731 size = tree_to_shwi (DECL_SIZE (fld));
2732 if (pos <= offset && (pos + size) > offset
2733 /* Do not get confused by zero sized bases. */
2734 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2735 break;
2737 /* Within a class type we should always find corresponding fields. */
2738 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2740 /* Nonbase types should have been stripped by outer_class_type. */
2741 gcc_assert (DECL_ARTIFICIAL (fld));
2743 outer_type = TREE_TYPE (fld);
2744 offset -= pos;
2746 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2747 offset, otr_type);
2748 if (!base_binfo)
2750 gcc_assert (odr_violation_reported);
2751 return;
2753 gcc_assert (base_binfo);
2754 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2756 bool can_refer;
2757 tree target = gimple_get_virt_method_for_binfo (otr_token,
2758 base_binfo,
2759 &can_refer);
2760 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2761 maybe_record_node (nodes, target, inserted, can_refer, completep);
2762 matched_vtables->add (BINFO_VTABLE (base_binfo));
2767 /* When virtual table is removed, we may need to flush the cache. */
2769 static void
2770 devirt_variable_node_removal_hook (varpool_node *n,
2771 void *d ATTRIBUTE_UNUSED)
2773 if (cached_polymorphic_call_targets
2774 && DECL_VIRTUAL_P (n->decl)
2775 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2776 free_polymorphic_call_targets_hash ();
2779 /* Record about how many calls would benefit from given type to be final. */
2781 struct odr_type_warn_count
2783 tree type;
2784 int count;
2785 gcov_type dyn_count;
2788 /* Record about how many calls would benefit from given method to be final. */
2790 struct decl_warn_count
2792 tree decl;
2793 int count;
2794 gcov_type dyn_count;
2797 /* Information about type and decl warnings. */
2799 struct final_warning_record
2801 gcov_type dyn_count;
2802 vec<odr_type_warn_count> type_warnings;
2803 hash_map<tree, decl_warn_count> decl_warnings;
2805 struct final_warning_record *final_warning_records;
2807 /* Return vector containing possible targets of polymorphic call of type
2808 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2809 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2810 OTR_TYPE and include their virtual method. This is useful for types
2811 possibly in construction or destruction where the virtual table may
2812 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2813 us to walk the inheritance graph for all derivations.
2815 If COMPLETEP is non-NULL, store true if the list is complete.
2816 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2817 in the target cache. If user needs to visit every target list
2818 just once, it can memoize them.
2820 If SPECULATIVE is set, the list will not contain targets that
2821 are not speculatively taken.
2823 Returned vector is placed into cache. It is NOT caller's responsibility
2824 to free it. The vector can be freed on cgraph_remove_node call if
2825 the particular node is a virtual function present in the cache. */
2827 vec <cgraph_node *>
2828 possible_polymorphic_call_targets (tree otr_type,
2829 HOST_WIDE_INT otr_token,
2830 ipa_polymorphic_call_context context,
2831 bool *completep,
2832 void **cache_token,
2833 bool speculative)
2835 static struct cgraph_node_hook_list *node_removal_hook_holder;
2836 vec <cgraph_node *> nodes = vNULL;
2837 auto_vec <tree, 8> bases_to_consider;
2838 odr_type type, outer_type;
2839 polymorphic_call_target_d key;
2840 polymorphic_call_target_d **slot;
2841 unsigned int i;
2842 tree binfo, target;
2843 bool complete;
2844 bool can_refer = false;
2845 bool skipped = false;
2847 otr_type = TYPE_MAIN_VARIANT (otr_type);
2849 /* If ODR is not initialized or the context is invalid, return empty
2850 incomplete list. */
2851 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
2853 if (completep)
2854 *completep = context.invalid;
2855 if (cache_token)
2856 *cache_token = NULL;
2857 return nodes;
2860 /* Do not bother to compute speculative info when user do not asks for it. */
2861 if (!speculative || !context.speculative_outer_type)
2862 context.clear_speculation ();
2864 type = get_odr_type (otr_type, true);
2866 /* Recording type variants would waste results cache. */
2867 gcc_assert (!context.outer_type
2868 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2870 /* Look up the outer class type we want to walk.
2871 If we fail to do so, the context is invalid. */
2872 if ((context.outer_type || context.speculative_outer_type)
2873 && !context.restrict_to_inner_class (otr_type))
2875 if (completep)
2876 *completep = true;
2877 if (cache_token)
2878 *cache_token = NULL;
2879 return nodes;
2881 gcc_assert (!context.invalid);
2883 /* Check that restrict_to_inner_class kept the main variant. */
2884 gcc_assert (!context.outer_type
2885 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2887 /* We canonicalize our query, so we do not need extra hashtable entries. */
2889 /* Without outer type, we have no use for offset. Just do the
2890 basic search from inner type. */
2891 if (!context.outer_type)
2892 context.clear_outer_type (otr_type);
2893 /* We need to update our hierarchy if the type does not exist. */
2894 outer_type = get_odr_type (context.outer_type, true);
2895 /* If the type is complete, there are no derivations. */
2896 if (TYPE_FINAL_P (outer_type->type))
2897 context.maybe_derived_type = false;
2899 /* Initialize query cache. */
2900 if (!cached_polymorphic_call_targets)
2902 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
2903 polymorphic_call_target_hash
2904 = new polymorphic_call_target_hash_type (23);
2905 if (!node_removal_hook_holder)
2907 node_removal_hook_holder =
2908 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
2909 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
2910 NULL);
2914 if (in_lto_p)
2916 if (context.outer_type != otr_type)
2917 context.outer_type
2918 = get_odr_type (context.outer_type, true)->type;
2919 if (context.speculative_outer_type)
2920 context.speculative_outer_type
2921 = get_odr_type (context.speculative_outer_type, true)->type;
2924 /* Look up cached answer. */
2925 key.type = type;
2926 key.otr_token = otr_token;
2927 key.speculative = speculative;
2928 key.context = context;
2929 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
2930 if (cache_token)
2931 *cache_token = (void *)*slot;
2932 if (*slot)
2934 if (completep)
2935 *completep = (*slot)->complete;
2936 if ((*slot)->type_warning && final_warning_records)
2938 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
2939 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
2940 += final_warning_records->dyn_count;
2942 if (!speculative && (*slot)->decl_warning && final_warning_records)
2944 struct decl_warn_count *c =
2945 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
2946 c->count++;
2947 c->dyn_count += final_warning_records->dyn_count;
2949 return (*slot)->targets;
2952 complete = true;
2954 /* Do actual search. */
2955 timevar_push (TV_IPA_VIRTUAL_CALL);
2956 *slot = XCNEW (polymorphic_call_target_d);
2957 if (cache_token)
2958 *cache_token = (void *)*slot;
2959 (*slot)->type = type;
2960 (*slot)->otr_token = otr_token;
2961 (*slot)->context = context;
2962 (*slot)->speculative = speculative;
2964 hash_set<tree> inserted;
2965 hash_set<tree> matched_vtables;
2967 /* First insert targets we speculatively identified as likely. */
2968 if (context.speculative_outer_type)
2970 odr_type speculative_outer_type;
2971 bool speculation_complete = true;
2973 /* First insert target from type itself and check if it may have
2974 derived types. */
2975 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
2976 if (TYPE_FINAL_P (speculative_outer_type->type))
2977 context.speculative_maybe_derived_type = false;
2978 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
2979 context.speculative_offset, otr_type);
2980 if (binfo)
2981 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
2982 &can_refer);
2983 else
2984 target = NULL;
2986 /* In the case we get complete method, we don't need
2987 to walk derivations. */
2988 if (target && DECL_FINAL_P (target))
2989 context.speculative_maybe_derived_type = false;
2990 if (type_possibly_instantiated_p (speculative_outer_type->type))
2991 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
2992 if (binfo)
2993 matched_vtables.add (BINFO_VTABLE (binfo));
2996 /* Next walk recursively all derived types. */
2997 if (context.speculative_maybe_derived_type)
2998 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
2999 possible_polymorphic_call_targets_1 (nodes, &inserted,
3000 &matched_vtables,
3001 otr_type,
3002 speculative_outer_type->derived_types[i],
3003 otr_token, speculative_outer_type->type,
3004 context.speculative_offset,
3005 &speculation_complete,
3006 bases_to_consider,
3007 false);
3010 if (!speculative || !nodes.length ())
3012 /* First see virtual method of type itself. */
3013 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3014 context.offset, otr_type);
3015 if (binfo)
3016 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3017 &can_refer);
3018 else
3020 gcc_assert (odr_violation_reported);
3021 target = NULL;
3024 /* Destructors are never called through construction virtual tables,
3025 because the type is always known. */
3026 if (target && DECL_CXX_DESTRUCTOR_P (target))
3027 context.maybe_in_construction = false;
3029 if (target)
3031 /* In the case we get complete method, we don't need
3032 to walk derivations. */
3033 if (DECL_FINAL_P (target))
3034 context.maybe_derived_type = false;
3037 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3038 if (type_possibly_instantiated_p (outer_type->type))
3039 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3040 else
3041 skipped = true;
3043 if (binfo)
3044 matched_vtables.add (BINFO_VTABLE (binfo));
3046 /* Next walk recursively all derived types. */
3047 if (context.maybe_derived_type)
3049 for (i = 0; i < outer_type->derived_types.length(); i++)
3050 possible_polymorphic_call_targets_1 (nodes, &inserted,
3051 &matched_vtables,
3052 otr_type,
3053 outer_type->derived_types[i],
3054 otr_token, outer_type->type,
3055 context.offset, &complete,
3056 bases_to_consider,
3057 context.maybe_in_construction);
3059 if (!outer_type->all_derivations_known)
3061 if (!speculative && final_warning_records)
3063 if (complete
3064 && nodes.length () == 1
3065 && warn_suggest_final_types
3066 && !outer_type->derived_types.length ())
3068 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
3069 final_warning_records->type_warnings.safe_grow_cleared
3070 (odr_types.length ());
3071 final_warning_records->type_warnings[outer_type->id].count++;
3072 final_warning_records->type_warnings[outer_type->id].dyn_count
3073 += final_warning_records->dyn_count;
3074 final_warning_records->type_warnings[outer_type->id].type
3075 = outer_type->type;
3076 (*slot)->type_warning = outer_type->id + 1;
3078 if (complete
3079 && warn_suggest_final_methods
3080 && nodes.length () == 1
3081 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3082 outer_type->type))
3084 bool existed;
3085 struct decl_warn_count &c =
3086 final_warning_records->decl_warnings.get_or_insert
3087 (nodes[0]->decl, &existed);
3089 if (existed)
3091 c.count++;
3092 c.dyn_count += final_warning_records->dyn_count;
3094 else
3096 c.count = 1;
3097 c.dyn_count = final_warning_records->dyn_count;
3098 c.decl = nodes[0]->decl;
3100 (*slot)->decl_warning = nodes[0]->decl;
3103 complete = false;
3107 if (!speculative)
3109 /* Destructors are never called through construction virtual tables,
3110 because the type is always known. One of entries may be
3111 cxa_pure_virtual so look to at least two of them. */
3112 if (context.maybe_in_construction)
3113 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3114 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3115 context.maybe_in_construction = false;
3116 if (context.maybe_in_construction)
3118 if (type != outer_type
3119 && (!skipped
3120 || (context.maybe_derived_type
3121 && !type_all_derivations_known_p (outer_type->type))))
3122 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3123 context.offset, nodes, &inserted,
3124 &matched_vtables, &complete);
3125 if (skipped)
3126 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3127 for (i = 0; i < bases_to_consider.length(); i++)
3128 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3133 (*slot)->targets = nodes;
3134 (*slot)->complete = complete;
3135 if (completep)
3136 *completep = complete;
3138 timevar_pop (TV_IPA_VIRTUAL_CALL);
3139 return nodes;
3142 bool
3143 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3144 vec<const decl_warn_count*> *vec)
3146 vec->safe_push (&value);
3147 return true;
3150 /* Dump target list TARGETS into FILE. */
3152 static void
3153 dump_targets (FILE *f, vec <cgraph_node *> targets)
3155 unsigned int i;
3157 for (i = 0; i < targets.length (); i++)
3159 char *name = NULL;
3160 if (in_lto_p)
3161 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3162 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
3163 if (in_lto_p)
3164 free (name);
3165 if (!targets[i]->definition)
3166 fprintf (f, " (no definition%s)",
3167 DECL_DECLARED_INLINE_P (targets[i]->decl)
3168 ? " inline" : "");
3170 fprintf (f, "\n");
3173 /* Dump all possible targets of a polymorphic call. */
3175 void
3176 dump_possible_polymorphic_call_targets (FILE *f,
3177 tree otr_type,
3178 HOST_WIDE_INT otr_token,
3179 const ipa_polymorphic_call_context &ctx)
3181 vec <cgraph_node *> targets;
3182 bool final;
3183 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3184 unsigned int len;
3186 if (!type)
3187 return;
3188 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3189 ctx,
3190 &final, NULL, false);
3191 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3192 print_generic_expr (f, type->type, TDF_SLIM);
3193 fprintf (f, " token %i\n", (int)otr_token);
3195 ctx.dump (f);
3197 fprintf (f, " %s%s%s%s\n ",
3198 final ? "This is a complete list." :
3199 "This is partial list; extra targets may be defined in other units.",
3200 ctx.maybe_in_construction ? " (base types included)" : "",
3201 ctx.maybe_derived_type ? " (derived types included)" : "",
3202 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3203 len = targets.length ();
3204 dump_targets (f, targets);
3206 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3207 ctx,
3208 &final, NULL, true);
3209 if (targets.length () != len)
3211 fprintf (f, " Speculative targets:");
3212 dump_targets (f, targets);
3214 gcc_assert (targets.length () <= len);
3215 fprintf (f, "\n");
3219 /* Return true if N can be possibly target of a polymorphic call of
3220 OTR_TYPE/OTR_TOKEN. */
3222 bool
3223 possible_polymorphic_call_target_p (tree otr_type,
3224 HOST_WIDE_INT otr_token,
3225 const ipa_polymorphic_call_context &ctx,
3226 struct cgraph_node *n)
3228 vec <cgraph_node *> targets;
3229 unsigned int i;
3230 enum built_in_function fcode;
3231 bool final;
3233 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3234 && ((fcode = DECL_FUNCTION_CODE (n->decl))
3235 == BUILT_IN_UNREACHABLE
3236 || fcode == BUILT_IN_TRAP))
3237 return true;
3239 if (!odr_hash)
3240 return true;
3241 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3242 for (i = 0; i < targets.length (); i++)
3243 if (n->semantically_equivalent_p (targets[i]))
3244 return true;
3246 /* At a moment we allow middle end to dig out new external declarations
3247 as a targets of polymorphic calls. */
3248 if (!final && !n->definition)
3249 return true;
3250 return false;
3255 /* Return true if N can be possibly target of a polymorphic call of
3256 OBJ_TYPE_REF expression REF in STMT. */
3258 bool
3259 possible_polymorphic_call_target_p (tree ref,
3260 gimple stmt,
3261 struct cgraph_node *n)
3263 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3264 tree call_fn = gimple_call_fn (stmt);
3266 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3267 tree_to_uhwi
3268 (OBJ_TYPE_REF_TOKEN (call_fn)),
3269 context,
3274 /* After callgraph construction new external nodes may appear.
3275 Add them into the graph. */
3277 void
3278 update_type_inheritance_graph (void)
3280 struct cgraph_node *n;
3282 if (!odr_hash)
3283 return;
3284 free_polymorphic_call_targets_hash ();
3285 timevar_push (TV_IPA_INHERITANCE);
3286 /* We reconstruct the graph starting from types of all methods seen in the
3287 the unit. */
3288 FOR_EACH_FUNCTION (n)
3289 if (DECL_VIRTUAL_P (n->decl)
3290 && !n->definition
3291 && n->real_symbol_p ())
3292 get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n->decl))),
3293 true);
3294 timevar_pop (TV_IPA_INHERITANCE);
3298 /* Return true if N looks like likely target of a polymorphic call.
3299 Rule out cxa_pure_virtual, noreturns, function declared cold and
3300 other obvious cases. */
3302 bool
3303 likely_target_p (struct cgraph_node *n)
3305 int flags;
3306 /* cxa_pure_virtual and similar things are not likely. */
3307 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3308 return false;
3309 flags = flags_from_decl_or_type (n->decl);
3310 if (flags & ECF_NORETURN)
3311 return false;
3312 if (lookup_attribute ("cold",
3313 DECL_ATTRIBUTES (n->decl)))
3314 return false;
3315 if (n->frequency < NODE_FREQUENCY_NORMAL)
3316 return false;
3317 /* If there are no live virtual tables referring the target,
3318 the only way the target can be called is an instance coming from other
3319 compilation unit; speculative devirtualization is built around an
3320 assumption that won't happen. */
3321 if (!referenced_from_vtable_p (n))
3322 return false;
3323 return true;
3326 /* Compare type warning records P1 and P2 and choose one with larger count;
3327 helper for qsort. */
3330 type_warning_cmp (const void *p1, const void *p2)
3332 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3333 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3335 if (t1->dyn_count < t2->dyn_count)
3336 return 1;
3337 if (t1->dyn_count > t2->dyn_count)
3338 return -1;
3339 return t2->count - t1->count;
3342 /* Compare decl warning records P1 and P2 and choose one with larger count;
3343 helper for qsort. */
3346 decl_warning_cmp (const void *p1, const void *p2)
3348 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3349 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3351 if (t1->dyn_count < t2->dyn_count)
3352 return 1;
3353 if (t1->dyn_count > t2->dyn_count)
3354 return -1;
3355 return t2->count - t1->count;
3359 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3360 context CTX. */
3362 struct cgraph_node *
3363 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3364 ipa_polymorphic_call_context ctx)
3366 vec <cgraph_node *>targets
3367 = possible_polymorphic_call_targets
3368 (otr_type, otr_token, ctx, NULL, NULL, true);
3369 unsigned int i;
3370 struct cgraph_node *likely_target = NULL;
3372 for (i = 0; i < targets.length (); i++)
3373 if (likely_target_p (targets[i]))
3375 if (likely_target)
3376 return NULL;
3377 likely_target = targets[i];
3379 if (!likely_target
3380 ||!likely_target->definition
3381 || DECL_EXTERNAL (likely_target->decl))
3382 return NULL;
3384 /* Don't use an implicitly-declared destructor (c++/58678). */
3385 struct cgraph_node *non_thunk_target
3386 = likely_target->function_symbol ();
3387 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3388 return NULL;
3389 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3390 && likely_target->can_be_discarded_p ())
3391 return NULL;
3392 return likely_target;
3395 /* The ipa-devirt pass.
3396 When polymorphic call has only one likely target in the unit,
3397 turn it into a speculative call. */
3399 static unsigned int
3400 ipa_devirt (void)
3402 struct cgraph_node *n;
3403 hash_set<void *> bad_call_targets;
3404 struct cgraph_edge *e;
3406 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3407 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3408 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3409 int ndropped = 0;
3411 if (!odr_types_ptr)
3412 return 0;
3414 if (dump_file)
3415 dump_type_inheritance_graph (dump_file);
3417 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3418 This is implemented by setting up final_warning_records that are updated
3419 by get_polymorphic_call_targets.
3420 We need to clear cache in this case to trigger recomputation of all
3421 entries. */
3422 if (warn_suggest_final_methods || warn_suggest_final_types)
3424 final_warning_records = new (final_warning_record);
3425 final_warning_records->type_warnings = vNULL;
3426 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
3427 free_polymorphic_call_targets_hash ();
3430 FOR_EACH_DEFINED_FUNCTION (n)
3432 bool update = false;
3433 if (!opt_for_fn (n->decl, flag_devirtualize))
3434 continue;
3435 if (dump_file && n->indirect_calls)
3436 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
3437 n->name (), n->order);
3438 for (e = n->indirect_calls; e; e = e->next_callee)
3439 if (e->indirect_info->polymorphic)
3441 struct cgraph_node *likely_target = NULL;
3442 void *cache_token;
3443 bool final;
3445 if (final_warning_records)
3446 final_warning_records->dyn_count = e->count;
3448 vec <cgraph_node *>targets
3449 = possible_polymorphic_call_targets
3450 (e, &final, &cache_token, true);
3451 unsigned int i;
3453 /* Trigger warnings by calculating non-speculative targets. */
3454 if (warn_suggest_final_methods || warn_suggest_final_types)
3455 possible_polymorphic_call_targets (e);
3457 if (dump_file)
3458 dump_possible_polymorphic_call_targets
3459 (dump_file, e);
3461 npolymorphic++;
3463 /* See if the call can be devirtualized by means of ipa-prop's
3464 polymorphic call context propagation. If not, we can just
3465 forget about this call being polymorphic and avoid some heavy
3466 lifting in remove_unreachable_nodes that will otherwise try to
3467 keep all possible targets alive until inlining and in the inliner
3468 itself.
3470 This may need to be revisited once we add further ways to use
3471 the may edges, but it is a resonable thing to do right now. */
3473 if ((e->indirect_info->param_index == -1
3474 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3475 && e->indirect_info->vptr_changed))
3476 && !flag_ltrans_devirtualize)
3478 e->indirect_info->polymorphic = false;
3479 ndropped++;
3480 if (dump_file)
3481 fprintf (dump_file, "Dropping polymorphic call info;"
3482 " it can not be used by ipa-prop\n");
3485 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3486 continue;
3488 if (!e->maybe_hot_p ())
3490 if (dump_file)
3491 fprintf (dump_file, "Call is cold\n\n");
3492 ncold++;
3493 continue;
3495 if (e->speculative)
3497 if (dump_file)
3498 fprintf (dump_file, "Call is already speculated\n\n");
3499 nspeculated++;
3501 /* When dumping see if we agree with speculation. */
3502 if (!dump_file)
3503 continue;
3505 if (bad_call_targets.contains (cache_token))
3507 if (dump_file)
3508 fprintf (dump_file, "Target list is known to be useless\n\n");
3509 nmultiple++;
3510 continue;
3512 for (i = 0; i < targets.length (); i++)
3513 if (likely_target_p (targets[i]))
3515 if (likely_target)
3517 likely_target = NULL;
3518 if (dump_file)
3519 fprintf (dump_file, "More than one likely target\n\n");
3520 nmultiple++;
3521 break;
3523 likely_target = targets[i];
3525 if (!likely_target)
3527 bad_call_targets.add (cache_token);
3528 continue;
3530 /* This is reached only when dumping; check if we agree or disagree
3531 with the speculation. */
3532 if (e->speculative)
3534 struct cgraph_edge *e2;
3535 struct ipa_ref *ref;
3536 e->speculative_call_info (e2, e, ref);
3537 if (e2->callee->ultimate_alias_target ()
3538 == likely_target->ultimate_alias_target ())
3540 fprintf (dump_file, "We agree with speculation\n\n");
3541 nok++;
3543 else
3545 fprintf (dump_file, "We disagree with speculation\n\n");
3546 nwrong++;
3548 continue;
3550 if (!likely_target->definition)
3552 if (dump_file)
3553 fprintf (dump_file, "Target is not a definition\n\n");
3554 nnotdefined++;
3555 continue;
3557 /* Do not introduce new references to external symbols. While we
3558 can handle these just well, it is common for programs to
3559 incorrectly with headers defining methods they are linked
3560 with. */
3561 if (DECL_EXTERNAL (likely_target->decl))
3563 if (dump_file)
3564 fprintf (dump_file, "Target is external\n\n");
3565 nexternal++;
3566 continue;
3568 /* Don't use an implicitly-declared destructor (c++/58678). */
3569 struct cgraph_node *non_thunk_target
3570 = likely_target->function_symbol ();
3571 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3573 if (dump_file)
3574 fprintf (dump_file, "Target is artificial\n\n");
3575 nartificial++;
3576 continue;
3578 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3579 && likely_target->can_be_discarded_p ())
3581 if (dump_file)
3582 fprintf (dump_file, "Target is overwritable\n\n");
3583 noverwritable++;
3584 continue;
3586 else if (dbg_cnt (devirt))
3588 if (dump_enabled_p ())
3590 location_t locus = gimple_location_safe (e->call_stmt);
3591 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3592 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3593 n->name (), n->order,
3594 likely_target->name (),
3595 likely_target->order);
3597 if (!likely_target->can_be_discarded_p ())
3599 cgraph_node *alias;
3600 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3601 if (alias)
3602 likely_target = alias;
3604 nconverted++;
3605 update = true;
3606 e->make_speculative
3607 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
3610 if (update)
3611 inline_update_overall_summary (n);
3613 if (warn_suggest_final_methods || warn_suggest_final_types)
3615 if (warn_suggest_final_types)
3617 final_warning_records->type_warnings.qsort (type_warning_cmp);
3618 for (unsigned int i = 0;
3619 i < final_warning_records->type_warnings.length (); i++)
3620 if (final_warning_records->type_warnings[i].count)
3622 tree type = final_warning_records->type_warnings[i].type;
3623 int count = final_warning_records->type_warnings[i].count;
3624 long long dyn_count
3625 = final_warning_records->type_warnings[i].dyn_count;
3627 if (!dyn_count)
3628 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3629 OPT_Wsuggest_final_types, count,
3630 "Declaring type %qD final "
3631 "would enable devirtualization of %i call",
3632 "Declaring type %qD final "
3633 "would enable devirtualization of %i calls",
3634 type,
3635 count);
3636 else
3637 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3638 OPT_Wsuggest_final_types, count,
3639 "Declaring type %qD final "
3640 "would enable devirtualization of %i call "
3641 "executed %lli times",
3642 "Declaring type %qD final "
3643 "would enable devirtualization of %i calls "
3644 "executed %lli times",
3645 type,
3646 count,
3647 dyn_count);
3651 if (warn_suggest_final_methods)
3653 vec<const decl_warn_count*> decl_warnings_vec = vNULL;
3655 final_warning_records->decl_warnings.traverse
3656 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3657 decl_warnings_vec.qsort (decl_warning_cmp);
3658 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3660 tree decl = decl_warnings_vec[i]->decl;
3661 int count = decl_warnings_vec[i]->count;
3662 long long dyn_count = decl_warnings_vec[i]->dyn_count;
3664 if (!dyn_count)
3665 if (DECL_CXX_DESTRUCTOR_P (decl))
3666 warning_n (DECL_SOURCE_LOCATION (decl),
3667 OPT_Wsuggest_final_methods, count,
3668 "Declaring virtual destructor of %qD final "
3669 "would enable devirtualization of %i call",
3670 "Declaring virtual destructor of %qD final "
3671 "would enable devirtualization of %i calls",
3672 DECL_CONTEXT (decl), count);
3673 else
3674 warning_n (DECL_SOURCE_LOCATION (decl),
3675 OPT_Wsuggest_final_methods, count,
3676 "Declaring method %qD final "
3677 "would enable devirtualization of %i call",
3678 "Declaring method %qD final "
3679 "would enable devirtualization of %i calls",
3680 decl, count);
3681 else if (DECL_CXX_DESTRUCTOR_P (decl))
3682 warning_n (DECL_SOURCE_LOCATION (decl),
3683 OPT_Wsuggest_final_methods, count,
3684 "Declaring virtual destructor of %qD final "
3685 "would enable devirtualization of %i call "
3686 "executed %lli times",
3687 "Declaring virtual destructor of %qD final "
3688 "would enable devirtualization of %i calls "
3689 "executed %lli times",
3690 DECL_CONTEXT (decl), count, dyn_count);
3691 else
3692 warning_n (DECL_SOURCE_LOCATION (decl),
3693 OPT_Wsuggest_final_methods, count,
3694 "Declaring method %qD final "
3695 "would enable devirtualization of %i call "
3696 "executed %lli times",
3697 "Declaring method %qD final "
3698 "would enable devirtualization of %i calls "
3699 "executed %lli times",
3700 decl, count, dyn_count);
3704 delete (final_warning_records);
3705 final_warning_records = 0;
3708 if (dump_file)
3709 fprintf (dump_file,
3710 "%i polymorphic calls, %i devirtualized,"
3711 " %i speculatively devirtualized, %i cold\n"
3712 "%i have multiple targets, %i overwritable,"
3713 " %i already speculated (%i agree, %i disagree),"
3714 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3715 npolymorphic, ndevirtualized, nconverted, ncold,
3716 nmultiple, noverwritable, nspeculated, nok, nwrong,
3717 nexternal, nnotdefined, nartificial, ndropped);
3718 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3721 namespace {
3723 const pass_data pass_data_ipa_devirt =
3725 IPA_PASS, /* type */
3726 "devirt", /* name */
3727 OPTGROUP_NONE, /* optinfo_flags */
3728 TV_IPA_DEVIRT, /* tv_id */
3729 0, /* properties_required */
3730 0, /* properties_provided */
3731 0, /* properties_destroyed */
3732 0, /* todo_flags_start */
3733 ( TODO_dump_symtab ), /* todo_flags_finish */
3736 class pass_ipa_devirt : public ipa_opt_pass_d
3738 public:
3739 pass_ipa_devirt (gcc::context *ctxt)
3740 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3741 NULL, /* generate_summary */
3742 NULL, /* write_summary */
3743 NULL, /* read_summary */
3744 NULL, /* write_optimization_summary */
3745 NULL, /* read_optimization_summary */
3746 NULL, /* stmt_fixup */
3747 0, /* function_transform_todo_flags_start */
3748 NULL, /* function_transform */
3749 NULL) /* variable_transform */
3752 /* opt_pass methods: */
3753 virtual bool gate (function *)
3755 /* In LTO, always run the IPA passes and decide on function basis if the
3756 pass is enabled. */
3757 if (in_lto_p)
3758 return true;
3759 return (flag_devirtualize
3760 && (flag_devirtualize_speculatively
3761 || (warn_suggest_final_methods
3762 || warn_suggest_final_types))
3763 && optimize);
3766 virtual unsigned int execute (function *) { return ipa_devirt (); }
3768 }; // class pass_ipa_devirt
3770 } // anon namespace
3772 ipa_opt_pass_d *
3773 make_pass_ipa_devirt (gcc::context *ctxt)
3775 return new pass_ipa_devirt (ctxt);
3778 #include "gt-ipa-devirt.h"