Daily bump.
[official-gcc.git] / gcc / ipa-devirt.c
blobe878bc1206e94c7c142a03e0eba81e1229d2bff0
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 (AGGREGATE_TYPE_P (t1)
679 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
680 return false;
682 type_pair pair={t1,t2};
683 if (TYPE_UID (t1) > TYPE_UID (t2))
685 pair.first = t2;
686 pair.second = t1;
688 if (visited->add (pair))
689 return true;
690 return odr_types_equivalent_p (t1, t2, false, NULL, visited);
693 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
694 violation warnings. */
696 void
697 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
699 int n1, n2;
701 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
703 odr_violation_reported = true;
704 if (DECL_VIRTUAL_P (prevailing->decl))
706 varpool_node *tmp = prevailing;
707 prevailing = vtable;
708 vtable = tmp;
710 if (warning_at (DECL_SOURCE_LOCATION
711 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
712 OPT_Wodr,
713 "virtual table of type %qD violates one definition rule",
714 DECL_CONTEXT (vtable->decl)))
715 inform (DECL_SOURCE_LOCATION (prevailing->decl),
716 "variable of same assembler name as the virtual table is "
717 "defined in another translation unit");
718 return;
720 if (!prevailing->definition || !vtable->definition)
721 return;
723 /* If we do not stream ODR type info, do not bother to do useful compare. */
724 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
725 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
726 return;
728 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
730 if (class_type->odr_violated)
731 return;
733 for (n1 = 0, n2 = 0; true; n1++, n2++)
735 struct ipa_ref *ref1, *ref2;
736 bool end1, end2;
738 end1 = !prevailing->iterate_reference (n1, ref1);
739 end2 = !vtable->iterate_reference (n2, ref2);
741 /* !DECL_VIRTUAL_P means RTTI entry;
742 We warn when RTTI is lost because non-RTTI previals; we silently
743 accept the other case. */
744 while (!end2
745 && (end1
746 || (DECL_ASSEMBLER_NAME (ref1->referred->decl)
747 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
748 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
749 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
751 if (!class_type->rtti_broken
752 && warning_at (DECL_SOURCE_LOCATION
753 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
754 OPT_Wodr,
755 "virtual table of type %qD contains RTTI "
756 "information",
757 DECL_CONTEXT (vtable->decl)))
759 inform (DECL_SOURCE_LOCATION
760 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
761 "but is prevailed by one without from other translation "
762 "unit");
763 inform (DECL_SOURCE_LOCATION
764 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
765 "RTTI will not work on this type");
766 class_type->rtti_broken = true;
768 n2++;
769 end2 = !vtable->iterate_reference (n2, ref2);
771 while (!end1
772 && (end2
773 || (DECL_ASSEMBLER_NAME (ref2->referred->decl)
774 != DECL_ASSEMBLER_NAME (ref1->referred->decl)
775 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
776 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
778 n1++;
779 end1 = !prevailing->iterate_reference (n1, ref1);
782 /* Finished? */
783 if (end1 && end2)
785 /* Extra paranoia; compare the sizes. We do not have information
786 about virtual inheritance offsets, so just be sure that these
787 match.
788 Do this as very last check so the not very informative error
789 is not output too often. */
790 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
792 class_type->odr_violated = true;
793 if (warning_at (DECL_SOURCE_LOCATION
794 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
795 OPT_Wodr,
796 "virtual table of type %qD violates "
797 "one definition rule ",
798 DECL_CONTEXT (vtable->decl)))
800 inform (DECL_SOURCE_LOCATION
801 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
802 "the conflicting type defined in another translation "
803 "unit has virtual table of different size");
806 return;
809 if (!end1 && !end2)
811 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
812 == DECL_ASSEMBLER_NAME (ref2->referred->decl))
813 continue;
815 class_type->odr_violated = true;
817 /* If the loops above stopped on non-virtual pointer, we have
818 mismatch in RTTI information mangling. */
819 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
820 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
822 if (warning_at (DECL_SOURCE_LOCATION
823 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
824 OPT_Wodr,
825 "virtual table of type %qD violates "
826 "one definition rule ",
827 DECL_CONTEXT (vtable->decl)))
829 inform (DECL_SOURCE_LOCATION
830 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
831 "the conflicting type defined in another translation "
832 "unit with different RTTI information");
834 return;
836 /* At this point both REF1 and REF2 points either to virtual table
837 or virtual method. If one points to virtual table and other to
838 method we can complain the same way as if one table was shorter
839 than other pointing out the extra method. */
840 if (TREE_CODE (ref1->referred->decl)
841 != TREE_CODE (ref2->referred->decl))
843 if (TREE_CODE (ref1->referred->decl) == VAR_DECL)
844 end1 = true;
845 else if (TREE_CODE (ref2->referred->decl) == VAR_DECL)
846 end2 = true;
850 class_type->odr_violated = true;
852 /* Complain about size mismatch. Either we have too many virutal
853 functions or too many virtual table pointers. */
854 if (end1 || end2)
856 if (end1)
858 varpool_node *tmp = prevailing;
859 prevailing = vtable;
860 vtable = tmp;
861 ref1 = ref2;
863 if (warning_at (DECL_SOURCE_LOCATION
864 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
865 OPT_Wodr,
866 "virtual table of type %qD violates "
867 "one definition rule",
868 DECL_CONTEXT (vtable->decl)))
870 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
872 inform (DECL_SOURCE_LOCATION
873 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
874 "the conflicting type defined in another translation "
875 "unit");
876 inform (DECL_SOURCE_LOCATION
877 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
878 "contains additional virtual method %qD",
879 ref1->referred->decl);
881 else
883 inform (DECL_SOURCE_LOCATION
884 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
885 "the conflicting type defined in another translation "
886 "unit has virtual table table with more entries");
889 return;
892 /* And in the last case we have either mistmatch in between two virtual
893 methods or two virtual table pointers. */
894 if (warning_at (DECL_SOURCE_LOCATION
895 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
896 "virtual table of type %qD violates "
897 "one definition rule ",
898 DECL_CONTEXT (vtable->decl)))
900 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
902 inform (DECL_SOURCE_LOCATION
903 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
904 "the conflicting type defined in another translation "
905 "unit");
906 gcc_assert (TREE_CODE (ref2->referred->decl)
907 == FUNCTION_DECL);
908 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
909 "virtual method %qD", ref1->referred->decl);
910 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
911 "ought to match virtual method %qD but does not",
912 ref2->referred->decl);
914 else
915 inform (DECL_SOURCE_LOCATION
916 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
917 "the conflicting type defined in another translation "
918 "unit has virtual table table with different contents");
919 return;
924 /* Output ODR violation warning about T1 and T2 with REASON.
925 Display location of ST1 and ST2 if REASON speaks about field or
926 method of the type.
927 If WARN is false, do nothing. Set WARNED if warning was indeed
928 output. */
930 void
931 warn_odr (tree t1, tree t2, tree st1, tree st2,
932 bool warn, bool *warned, const char *reason)
934 tree decl2 = TYPE_NAME (t2);
935 if (warned)
936 *warned = false;
938 if (!warn || !TYPE_NAME(t1))
939 return;
941 /* ODR warnings are output druing LTO streaming; we must apply location
942 cache for potential warnings to be output correctly. */
943 if (lto_location_cache::current_cache)
944 lto_location_cache::current_cache->apply_location_cache ();
946 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
947 "type %qT violates one definition rule",
948 t1))
949 return;
950 if (!st1 && !st2)
952 /* For FIELD_DECL support also case where one of fields is
953 NULL - this is used when the structures have mismatching number of
954 elements. */
955 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
957 inform (DECL_SOURCE_LOCATION (decl2),
958 "a different type is defined in another translation unit");
959 if (!st1)
961 st1 = st2;
962 st2 = NULL;
964 inform (DECL_SOURCE_LOCATION (st1),
965 "the first difference of corresponding definitions is field %qD",
966 st1);
967 if (st2)
968 decl2 = st2;
970 else if (TREE_CODE (st1) == FUNCTION_DECL)
972 inform (DECL_SOURCE_LOCATION (decl2),
973 "a different type is defined in another translation unit");
974 inform (DECL_SOURCE_LOCATION (st1),
975 "the first difference of corresponding definitions is method %qD",
976 st1);
977 decl2 = st2;
979 else
980 return;
981 inform (DECL_SOURCE_LOCATION (decl2), reason);
983 if (warned)
984 *warned = true;
987 /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
988 because they are used on same place in ODR matching types.
989 They are not; inform the user. */
991 void
992 warn_types_mismatch (tree t1, tree t2)
994 /* If types have names and they are different, it is most informative to
995 output those. */
996 if (TYPE_NAME (t1) && TYPE_NAME (t2)
997 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
998 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
999 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
1000 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
1002 char *name1 = xstrdup (cplus_demangle
1003 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
1004 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1005 char *name2 = cplus_demangle
1006 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
1007 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1008 if (name1 && name2 && strcmp (name1, name2))
1010 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1011 "type name %<%s%> should match type name %<%s%>",
1012 name1, name2);
1013 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
1014 "the incompatible type is defined here");
1015 free (name1);
1016 return;
1018 free (name1);
1020 /* It is a quite common bug to reference anonymous namespace type in
1021 non-anonymous namespace class. */
1022 if (type_in_anonymous_namespace_p (t1)
1023 || type_in_anonymous_namespace_p (t2))
1025 if (!type_in_anonymous_namespace_p (t1))
1027 tree tmp = t1;;
1028 t1 = t2;
1029 t2 = tmp;
1031 if (TYPE_NAME (t1) && TYPE_NAME (t2))
1033 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1034 "type %qT defined in anonymous namespace can not match "
1035 "type %qT",
1036 t1, t2);
1037 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
1038 "the incompatible type defined in anonymous namespace in "
1039 "another translation unit");
1041 else
1042 inform (UNKNOWN_LOCATION,
1043 "types in anonymous namespace does not match across "
1044 "translation unit boundary");
1045 return;
1047 /* A tricky case are component types. Often they appear the same in source
1048 code and the mismatch is dragged in by type they are build from.
1049 Look for those differences in subtypes and try to be informative. In other
1050 cases just output nothing because the source code is probably different
1051 and in this case we already output a all necessary info. */
1052 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1054 if (TREE_CODE (t1) == TREE_CODE (t2))
1056 hash_set<type_pair,pair_traits> visited;
1057 if (TREE_CODE (t1) == ARRAY_TYPE
1058 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1060 tree i1 = TYPE_DOMAIN (t1);
1061 tree i2 = TYPE_DOMAIN (t2);
1063 if (i1 && i2
1064 && TYPE_MAX_VALUE (i1)
1065 && TYPE_MAX_VALUE (i2)
1066 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1067 TYPE_MAX_VALUE (i2), 0))
1069 inform (UNKNOWN_LOCATION,
1070 "array types have different bounds");
1071 return;
1074 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1075 && !odr_subtypes_equivalent_p (TREE_TYPE (t1),
1076 TREE_TYPE (t2),
1077 &visited))
1078 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1079 else if (TREE_CODE (t1) == METHOD_TYPE
1080 || TREE_CODE (t1) == FUNCTION_TYPE)
1082 tree parms1, parms2;
1083 int count = 1;
1085 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1086 &visited))
1088 inform (UNKNOWN_LOCATION, "return value type mismatch");
1089 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1090 return;
1092 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1093 parms1 && parms2;
1094 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1095 count++)
1097 if (!odr_subtypes_equivalent_p
1098 (TREE_VALUE (parms1), TREE_VALUE (parms2), &visited))
1100 inform (UNKNOWN_LOCATION,
1101 "type mismatch in parameter %i", count);
1102 warn_types_mismatch (TREE_VALUE (parms1),
1103 TREE_VALUE (parms2));
1104 return;
1107 if (parms1 || parms2)
1109 inform (UNKNOWN_LOCATION,
1110 "types have different parameter counts");
1111 return;
1115 return;
1117 /* This should not happen but if it does, the warning would not be helpful.
1118 TODO: turn it into assert next stage1. */
1119 if (TYPE_NAME (t1) == TYPE_NAME (t2))
1120 return;
1121 /* In Firefox it is a common bug to have same types but in
1122 different namespaces. Be a bit more informative on
1123 this. */
1124 if (TYPE_CONTEXT (t1) && TYPE_CONTEXT (t2)
1125 && (((TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL)
1126 != (TREE_CODE (TYPE_CONTEXT (t2)) == NAMESPACE_DECL))
1127 || (TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL
1128 && (DECL_NAME (TYPE_CONTEXT (t1)) !=
1129 DECL_NAME (TYPE_CONTEXT (t2))))))
1130 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1131 "type %qT should match type %qT but is defined "
1132 "in different namespace ",
1133 t1, t2);
1134 else if (types_odr_comparable (t1, t2, true)
1135 && types_same_for_odr (t1, t2, true))
1136 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1137 "type %qT should match type %qT that itself violate "
1138 "one definition rule",
1139 t1, t2);
1140 else
1141 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1142 "type %qT should match type %qT",
1143 t1, t2);
1144 if (DECL_SOURCE_LOCATION (TYPE_NAME (t2)) > BUILTINS_LOCATION)
1145 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
1146 "the incompatible type is defined here");
1149 /* Compare T1 and T2, report ODR violations if WARN is true and set
1150 WARNED to true if anything is reported. Return true if types match.
1151 If true is returned, the types are also compatible in the sense of
1152 gimple_canonical_types_compatible_p. */
1154 static bool
1155 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1156 hash_set<type_pair,pair_traits> *visited)
1158 /* Check first for the obvious case of pointer identity. */
1159 if (t1 == t2)
1160 return true;
1161 gcc_assert (!type_in_anonymous_namespace_p (t1));
1162 gcc_assert (!type_in_anonymous_namespace_p (t2));
1164 /* Can't be the same type if the types don't have the same code. */
1165 if (TREE_CODE (t1) != TREE_CODE (t2))
1167 warn_odr (t1, t2, NULL, NULL, warn, warned,
1168 G_("a different type is defined in another translation unit"));
1169 return false;
1172 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1174 warn_odr (t1, t2, NULL, NULL, warn, warned,
1175 G_("a type with different qualifiers is defined in another "
1176 "translation unit"));
1177 return false;
1180 if (comp_type_attributes (t1, t2) != 1)
1182 warn_odr (t1, t2, NULL, NULL, warn, warned,
1183 G_("a type with attributes "
1184 "is defined in another translation unit"));
1185 return false;
1188 if (TREE_CODE (t1) == ENUMERAL_TYPE
1189 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1191 tree v1, v2;
1192 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1193 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1195 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1197 warn_odr (t1, t2, NULL, NULL, warn, warned,
1198 G_("an enum with different value name"
1199 " is defined in another translation unit"));
1200 return false;
1202 if (TREE_VALUE (v1) != TREE_VALUE (v2)
1203 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
1204 DECL_INITIAL (TREE_VALUE (v2)), 0))
1206 warn_odr (t1, t2, NULL, NULL, warn, warned,
1207 G_("an enum with different values is defined"
1208 " in another translation unit"));
1209 return false;
1212 if (v1 || v2)
1214 warn_odr (t1, t2, NULL, NULL, warn, warned,
1215 G_("an enum with mismatching number of values "
1216 "is defined in another translation unit"));
1217 return false;
1221 /* Non-aggregate types can be handled cheaply. */
1222 if (INTEGRAL_TYPE_P (t1)
1223 || SCALAR_FLOAT_TYPE_P (t1)
1224 || FIXED_POINT_TYPE_P (t1)
1225 || TREE_CODE (t1) == VECTOR_TYPE
1226 || TREE_CODE (t1) == COMPLEX_TYPE
1227 || TREE_CODE (t1) == OFFSET_TYPE
1228 || POINTER_TYPE_P (t1))
1230 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1232 warn_odr (t1, t2, NULL, NULL, warn, warned,
1233 G_("a type with different precision is defined "
1234 "in another translation unit"));
1235 return false;
1237 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1239 warn_odr (t1, t2, NULL, NULL, warn, warned,
1240 G_("a type with different signedness is defined "
1241 "in another translation unit"));
1242 return false;
1245 if (TREE_CODE (t1) == INTEGER_TYPE
1246 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1248 /* char WRT uint_8? */
1249 warn_odr (t1, t2, NULL, NULL, warn, warned,
1250 G_("a different type is defined in another "
1251 "translation unit"));
1252 return false;
1255 /* For canonical type comparisons we do not want to build SCCs
1256 so we cannot compare pointed-to types. But we can, for now,
1257 require the same pointed-to type kind and match what
1258 useless_type_conversion_p would do. */
1259 if (POINTER_TYPE_P (t1))
1261 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1262 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1264 warn_odr (t1, t2, NULL, NULL, warn, warned,
1265 G_("it is defined as a pointer in different address "
1266 "space in another translation unit"));
1267 return false;
1270 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1272 warn_odr (t1, t2, NULL, NULL, warn, warned,
1273 G_("it is defined as a pointer to different type "
1274 "in another translation unit"));
1275 if (warn && warned)
1276 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1277 return false;
1281 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1282 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1284 /* Probably specific enough. */
1285 warn_odr (t1, t2, NULL, NULL, warn, warned,
1286 G_("a different type is defined "
1287 "in another translation unit"));
1288 if (warn && warned)
1289 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1290 return false;
1293 /* Do type-specific comparisons. */
1294 else switch (TREE_CODE (t1))
1296 case ARRAY_TYPE:
1298 /* Array types are the same if the element types are the same and
1299 the number of elements are the same. */
1300 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1302 warn_odr (t1, t2, NULL, NULL, warn, warned,
1303 G_("a different type is defined in another "
1304 "translation unit"));
1305 if (warn && warned)
1306 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1308 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1309 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1310 == TYPE_NONALIASED_COMPONENT (t2));
1312 tree i1 = TYPE_DOMAIN (t1);
1313 tree i2 = TYPE_DOMAIN (t2);
1315 /* For an incomplete external array, the type domain can be
1316 NULL_TREE. Check this condition also. */
1317 if (i1 == NULL_TREE || i2 == NULL_TREE)
1318 return true;
1320 tree min1 = TYPE_MIN_VALUE (i1);
1321 tree min2 = TYPE_MIN_VALUE (i2);
1322 tree max1 = TYPE_MAX_VALUE (i1);
1323 tree max2 = TYPE_MAX_VALUE (i2);
1325 /* In C++, minimums should be always 0. */
1326 gcc_assert (min1 == min2);
1327 if (!operand_equal_p (max1, max2, 0))
1329 warn_odr (t1, t2, NULL, NULL, warn, warned,
1330 G_("an array of different size is defined "
1331 "in another translation unit"));
1332 return false;
1335 break;
1337 case METHOD_TYPE:
1338 case FUNCTION_TYPE:
1339 /* Function types are the same if the return type and arguments types
1340 are the same. */
1341 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1343 warn_odr (t1, t2, NULL, NULL, warn, warned,
1344 G_("has different return value "
1345 "in another translation unit"));
1346 if (warn && warned)
1347 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1348 return false;
1351 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
1352 return true;
1353 else
1355 tree parms1, parms2;
1357 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1358 parms1 && parms2;
1359 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1361 if (!odr_subtypes_equivalent_p
1362 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited))
1364 warn_odr (t1, t2, NULL, NULL, warn, warned,
1365 G_("has different parameters in another "
1366 "translation unit"));
1367 if (warn && warned)
1368 warn_types_mismatch (TREE_VALUE (parms1),
1369 TREE_VALUE (parms2));
1370 return false;
1374 if (parms1 || parms2)
1376 warn_odr (t1, t2, NULL, NULL, warn, warned,
1377 G_("has different parameters "
1378 "in another translation unit"));
1379 return false;
1382 return true;
1385 case RECORD_TYPE:
1386 case UNION_TYPE:
1387 case QUAL_UNION_TYPE:
1389 tree f1, f2;
1391 /* For aggregate types, all the fields must be the same. */
1392 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1394 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1395 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1396 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1398 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1399 warn_odr (t1, t2, NULL, NULL, warn, warned,
1400 G_("a type defined in another translation unit "
1401 "is not polymorphic"));
1402 else
1403 warn_odr (t1, t2, NULL, NULL, warn, warned,
1404 G_("a type defined in another translation unit "
1405 "is polymorphic"));
1406 return false;
1408 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1409 f1 || f2;
1410 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1412 /* Skip non-fields. */
1413 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1414 f1 = TREE_CHAIN (f1);
1415 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1416 f2 = TREE_CHAIN (f2);
1417 if (!f1 || !f2)
1418 break;
1419 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1421 warn_odr (t1, t2, NULL, NULL, warn, warned,
1422 G_("a type with different virtual table pointers"
1423 " is defined in another translation unit"));
1424 return false;
1426 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1428 warn_odr (t1, t2, NULL, NULL, warn, warned,
1429 G_("a type with different bases is defined "
1430 "in another translation unit"));
1431 return false;
1433 if (DECL_NAME (f1) != DECL_NAME (f2)
1434 && !DECL_ARTIFICIAL (f1))
1436 warn_odr (t1, t2, f1, f2, warn, warned,
1437 G_("a field with different name is defined "
1438 "in another translation unit"));
1439 return false;
1441 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1442 TREE_TYPE (f2), visited))
1444 /* Do not warn about artificial fields and just go into
1445 generic field mismatch warning. */
1446 if (DECL_ARTIFICIAL (f1))
1447 break;
1449 warn_odr (t1, t2, f1, f2, warn, warned,
1450 G_("a field of same name but different type "
1451 "is defined in another translation unit"));
1452 if (warn && warned)
1453 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2));
1454 return false;
1456 if (!gimple_compare_field_offset (f1, f2))
1458 /* Do not warn about artificial fields and just go into
1459 generic field mismatch warning. */
1460 if (DECL_ARTIFICIAL (f1))
1461 break;
1462 warn_odr (t1, t2, f1, f2, warn, warned,
1463 G_("fields has different layout "
1464 "in another translation unit"));
1465 return false;
1467 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1468 == DECL_NONADDRESSABLE_P (f2));
1471 /* If one aggregate has more fields than the other, they
1472 are not the same. */
1473 if (f1 || f2)
1475 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1476 warn_odr (t1, t2, NULL, NULL, warn, warned,
1477 G_("a type with different virtual table pointers"
1478 " is defined in another translation unit"));
1479 else if ((f1 && DECL_ARTIFICIAL (f1))
1480 || (f2 && DECL_ARTIFICIAL (f2)))
1481 warn_odr (t1, t2, NULL, NULL, warn, warned,
1482 G_("a type with different bases is defined "
1483 "in another translation unit"));
1484 else
1485 warn_odr (t1, t2, f1, f2, warn, warned,
1486 G_("a type with different number of fields "
1487 "is defined in another translation unit"));
1489 return false;
1491 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
1492 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
1493 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
1495 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
1496 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
1497 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
1499 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
1501 warn_odr (t1, t2, f1, f2, warn, warned,
1502 G_("a different method of same type "
1503 "is defined in another translation unit"));
1504 return false;
1506 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1508 warn_odr (t1, t2, f1, f2, warn, warned,
1509 G_("s definition that differs by virtual "
1510 "keyword in another translation unit"));
1511 return false;
1513 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1515 warn_odr (t1, t2, f1, f2, warn, warned,
1516 G_("virtual table layout differs in another "
1517 "translation unit"));
1518 return false;
1520 if (odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
1522 warn_odr (t1, t2, f1, f2, warn, warned,
1523 G_("method with incompatible type is defined "
1524 "in another translation unit"));
1525 return false;
1528 if (f1 || f2)
1530 warn_odr (t1, t2, NULL, NULL, warn, warned,
1531 G_("a type with different number of methods "
1532 "is defined in another translation unit"));
1533 return false;
1537 break;
1539 case VOID_TYPE:
1540 case NULLPTR_TYPE:
1541 break;
1543 default:
1544 debug_tree (t1);
1545 gcc_unreachable ();
1548 /* Those are better to come last as they are utterly uninformative. */
1549 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1550 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1552 warn_odr (t1, t2, NULL, NULL, warn, warned,
1553 G_("a type with different size "
1554 "is defined in another translation unit"));
1555 return false;
1557 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1558 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1560 warn_odr (t1, t2, NULL, NULL, warn, warned,
1561 G_("a type with different alignment "
1562 "is defined in another translation unit"));
1563 return false;
1565 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1566 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1567 TYPE_SIZE_UNIT (t2), 0));
1568 return true;
1571 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1572 from VAL->type. This may happen in LTO where tree merging did not merge
1573 all variants of the same type or due to ODR violation.
1575 Analyze and report ODR violations and add type to duplicate list.
1576 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1577 this is first time we see definition of a class return true so the
1578 base types are analyzed. */
1580 static bool
1581 add_type_duplicate (odr_type val, tree type)
1583 bool build_bases = false;
1584 bool prevail = false;
1585 bool odr_must_violate = false;
1587 if (!val->types_set)
1588 val->types_set = new hash_set<tree>;
1590 /* Chose polymorphic type as leader (this happens only in case of ODR
1591 violations. */
1592 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1593 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1594 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1595 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1597 prevail = true;
1598 build_bases = true;
1600 /* Always prefer complete type to be the leader. */
1601 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1603 prevail = true;
1604 build_bases = TYPE_BINFO (type);
1606 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1608 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1609 && TREE_CODE (type) == ENUMERAL_TYPE
1610 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1611 prevail = true;
1612 else if (TREE_CODE (val->type) == RECORD_TYPE
1613 && TREE_CODE (type) == RECORD_TYPE
1614 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1616 gcc_assert (!val->bases.length ());
1617 build_bases = true;
1618 prevail = true;
1621 if (prevail)
1623 tree tmp = type;
1625 type = val->type;
1626 val->type = tmp;
1629 val->types_set->add (type);
1631 /* If we now have a mangled name, be sure to record it to val->type
1632 so ODR hash can work. */
1634 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1635 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1636 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1638 bool merge = true;
1639 bool base_mismatch = false;
1640 unsigned int i;
1641 bool warned = false;
1642 hash_set<type_pair,pair_traits> visited;
1644 gcc_assert (in_lto_p);
1645 vec_safe_push (val->types, type);
1647 /* If both are class types, compare the bases. */
1648 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1649 && TREE_CODE (val->type) == RECORD_TYPE
1650 && TREE_CODE (type) == RECORD_TYPE
1651 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1653 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1654 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1656 if (!flag_ltrans && !warned && !val->odr_violated)
1658 tree extra_base;
1659 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1660 "a type with the same name but different "
1661 "number of polymorphic bases is "
1662 "defined in another translation unit");
1663 if (warned)
1665 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1666 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1667 extra_base = BINFO_BASE_BINFO
1668 (TYPE_BINFO (type),
1669 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1670 else
1671 extra_base = BINFO_BASE_BINFO
1672 (TYPE_BINFO (val->type),
1673 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1674 tree extra_base_type = BINFO_TYPE (extra_base);
1675 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1676 "the extra base is defined here");
1679 base_mismatch = true;
1681 else
1682 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1684 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1685 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1686 tree type1 = BINFO_TYPE (base1);
1687 tree type2 = BINFO_TYPE (base2);
1689 if (types_odr_comparable (type1, type2))
1691 if (!types_same_for_odr (type1, type2))
1692 base_mismatch = true;
1694 else
1696 hash_set<type_pair,pair_traits> visited;
1697 if (!odr_types_equivalent_p (type1, type2, false, NULL,
1698 &visited))
1699 base_mismatch = true;
1701 if (base_mismatch)
1703 if (!warned && !val->odr_violated)
1705 warn_odr (type, val->type, NULL, NULL,
1706 !warned, &warned,
1707 "a type with the same name but different base "
1708 "type is defined in another translation unit");
1709 if (warned)
1710 warn_types_mismatch (type1, type2);
1712 break;
1714 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1716 base_mismatch = true;
1717 if (!warned && !val->odr_violated)
1718 warn_odr (type, val->type, NULL, NULL,
1719 !warned, &warned,
1720 "a type with the same name but different base "
1721 "layout is defined in another translation unit");
1722 break;
1724 /* One of bases is not of complete type. */
1725 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1727 /* If we have a polymorphic type info specified for TYPE1
1728 but not for TYPE2 we possibly missed a base when recording
1729 VAL->type earlier.
1730 Be sure this does not happen. */
1731 if (TYPE_BINFO (type1)
1732 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1733 && !build_bases)
1734 odr_must_violate = true;
1735 break;
1737 /* One base is polymorphic and the other not.
1738 This ought to be diagnosed earlier, but do not ICE in the
1739 checking bellow. */
1740 else if (TYPE_BINFO (type1)
1741 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1742 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1744 if (!warned && !val->odr_violated)
1745 warn_odr (type, val->type, NULL, NULL,
1746 !warned, &warned,
1747 "a base of the type is polymorphic only in one "
1748 "translation unit");
1749 base_mismatch = true;
1750 break;
1753 if (base_mismatch)
1755 merge = false;
1756 odr_violation_reported = true;
1757 val->odr_violated = true;
1759 if (symtab->dump_file)
1761 fprintf (symtab->dump_file, "ODR base violation\n");
1763 print_node (symtab->dump_file, "", val->type, 0);
1764 putc ('\n',symtab->dump_file);
1765 print_node (symtab->dump_file, "", type, 0);
1766 putc ('\n',symtab->dump_file);
1771 /* Next compare memory layout. */
1772 if (!odr_types_equivalent_p (val->type, type,
1773 !flag_ltrans && !val->odr_violated && !warned,
1774 &warned, &visited))
1776 merge = false;
1777 odr_violation_reported = true;
1778 val->odr_violated = true;
1779 if (symtab->dump_file)
1781 fprintf (symtab->dump_file, "ODR violation\n");
1783 print_node (symtab->dump_file, "", val->type, 0);
1784 putc ('\n',symtab->dump_file);
1785 print_node (symtab->dump_file, "", type, 0);
1786 putc ('\n',symtab->dump_file);
1789 gcc_assert (val->odr_violated || !odr_must_violate);
1790 /* Sanity check that all bases will be build same way again. */
1791 #ifdef ENABLE_CHECKING
1792 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1793 && TREE_CODE (val->type) == RECORD_TYPE
1794 && TREE_CODE (type) == RECORD_TYPE
1795 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1796 && !val->odr_violated
1797 && !base_mismatch && val->bases.length ())
1799 unsigned int num_poly_bases = 0;
1800 unsigned int j;
1802 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1803 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1804 (TYPE_BINFO (type), i)))
1805 num_poly_bases++;
1806 gcc_assert (num_poly_bases == val->bases.length ());
1807 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
1808 i++)
1809 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1810 (TYPE_BINFO (type), i)))
1812 odr_type base = get_odr_type
1813 (BINFO_TYPE
1814 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1815 i)),
1816 true);
1817 gcc_assert (val->bases[j] == base);
1818 j++;
1821 #endif
1824 /* Regularize things a little. During LTO same types may come with
1825 different BINFOs. Either because their virtual table was
1826 not merged by tree merging and only later at decl merging or
1827 because one type comes with external vtable, while other
1828 with internal. We want to merge equivalent binfos to conserve
1829 memory and streaming overhead.
1831 The external vtables are more harmful: they contain references
1832 to external declarations of methods that may be defined in the
1833 merged LTO unit. For this reason we absolutely need to remove
1834 them and replace by internal variants. Not doing so will lead
1835 to incomplete answers from possible_polymorphic_call_targets.
1837 FIXME: disable for now; because ODR types are now build during
1838 streaming in, the variants do not need to be linked to the type,
1839 yet. We need to do the merging in cleanup pass to be implemented
1840 soon. */
1841 if (!flag_ltrans && merge
1842 && 0
1843 && TREE_CODE (val->type) == RECORD_TYPE
1844 && TREE_CODE (type) == RECORD_TYPE
1845 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1846 && TYPE_MAIN_VARIANT (type) == type
1847 && TYPE_MAIN_VARIANT (val->type) == val->type
1848 && BINFO_VTABLE (TYPE_BINFO (val->type))
1849 && BINFO_VTABLE (TYPE_BINFO (type)))
1851 tree master_binfo = TYPE_BINFO (val->type);
1852 tree v1 = BINFO_VTABLE (master_binfo);
1853 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1855 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1857 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1858 && operand_equal_p (TREE_OPERAND (v1, 1),
1859 TREE_OPERAND (v2, 1), 0));
1860 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1861 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1863 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1864 == DECL_ASSEMBLER_NAME (v2));
1866 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1868 unsigned int i;
1870 set_type_binfo (val->type, TYPE_BINFO (type));
1871 for (i = 0; i < val->types->length (); i++)
1873 if (TYPE_BINFO ((*val->types)[i])
1874 == master_binfo)
1875 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1877 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1879 else
1880 set_type_binfo (type, master_binfo);
1882 return build_bases;
1885 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1886 possibly new entry. */
1888 odr_type
1889 get_odr_type (tree type, bool insert)
1891 odr_type_d **slot = NULL;
1892 odr_type_d **vtable_slot = NULL;
1893 odr_type val = NULL;
1894 hashval_t hash;
1895 bool build_bases = false;
1896 bool insert_to_odr_array = false;
1897 int base_id = -1;
1899 type = main_odr_variant (type);
1901 gcc_checking_assert (can_be_name_hashed_p (type)
1902 || can_be_vtable_hashed_p (type));
1904 /* Lookup entry, first try name hash, fallback to vtable hash. */
1905 if (can_be_name_hashed_p (type))
1907 hash = hash_odr_name (type);
1908 slot = odr_hash->find_slot_with_hash (type, hash,
1909 insert ? INSERT : NO_INSERT);
1911 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
1913 hash = hash_odr_vtable (type);
1914 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
1915 insert ? INSERT : NO_INSERT);
1918 if (!slot && !vtable_slot)
1919 return NULL;
1921 /* See if we already have entry for type. */
1922 if ((slot && *slot) || (vtable_slot && *vtable_slot))
1924 if (slot && *slot)
1926 val = *slot;
1927 #ifdef ENABLE_CHECKING
1928 if (in_lto_p && can_be_vtable_hashed_p (type))
1930 hash = hash_odr_vtable (type);
1931 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
1932 NO_INSERT);
1933 gcc_assert (!vtable_slot || *vtable_slot == *slot);
1934 vtable_slot = NULL;
1936 #endif
1938 else if (*vtable_slot)
1939 val = *vtable_slot;
1941 if (val->type != type
1942 && (!val->types_set || !val->types_set->add (type)))
1944 gcc_assert (insert);
1945 /* We have type duplicate, but it may introduce vtable name or
1946 mangled name; be sure to keep hashes in sync. */
1947 if (in_lto_p && can_be_vtable_hashed_p (type)
1948 && (!vtable_slot || !*vtable_slot))
1950 if (!vtable_slot)
1952 hash = hash_odr_vtable (type);
1953 vtable_slot = odr_vtable_hash->find_slot_with_hash
1954 (type, hash, INSERT);
1955 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
1957 *vtable_slot = val;
1959 if (slot && !*slot)
1960 *slot = val;
1961 build_bases = add_type_duplicate (val, type);
1964 else
1966 val = ggc_cleared_alloc<odr_type_d> ();
1967 val->type = type;
1968 val->bases = vNULL;
1969 val->derived_types = vNULL;
1970 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
1971 build_bases = COMPLETE_TYPE_P (val->type);
1972 insert_to_odr_array = true;
1973 if (slot)
1974 *slot = val;
1975 if (vtable_slot)
1976 *vtable_slot = val;
1979 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1980 && type == TYPE_MAIN_VARIANT (type))
1982 tree binfo = TYPE_BINFO (type);
1983 unsigned int i;
1985 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
1987 val->all_derivations_known = type_all_derivations_known_p (type);
1988 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
1989 /* For now record only polymorphic types. other are
1990 pointless for devirtualization and we can not precisely
1991 determine ODR equivalency of these during LTO. */
1992 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
1994 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
1995 odr_type base = get_odr_type (base_type, true);
1996 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
1997 base->derived_types.safe_push (val);
1998 val->bases.safe_push (base);
1999 if (base->id > base_id)
2000 base_id = base->id;
2003 /* Ensure that type always appears after bases. */
2004 if (insert_to_odr_array)
2006 if (odr_types_ptr)
2007 val->id = odr_types.length ();
2008 vec_safe_push (odr_types_ptr, val);
2010 else if (base_id > val->id)
2012 odr_types[val->id] = 0;
2013 /* Be sure we did not recorded any derived types; these may need
2014 renumbering too. */
2015 gcc_assert (val->derived_types.length() == 0);
2016 if (odr_types_ptr)
2017 val->id = odr_types.length ();
2018 vec_safe_push (odr_types_ptr, val);
2020 return val;
2023 /* Add TYPE od ODR type hash. */
2025 void
2026 register_odr_type (tree type)
2028 if (!odr_hash)
2030 odr_hash = new odr_hash_type (23);
2031 if (in_lto_p)
2032 odr_vtable_hash = new odr_vtable_hash_type (23);
2034 /* Arrange things to be nicer and insert main variants first.
2035 ??? fundamental prerecorded types do not have mangled names; this
2036 makes it possible that non-ODR type is main_odr_variant of ODR type.
2037 Things may get smoother if LTO FE set mangled name of those types same
2038 way as C++ FE does. */
2039 if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type))))
2040 get_odr_type (TYPE_MAIN_VARIANT (type), true);
2041 if (TYPE_MAIN_VARIANT (type) != type && odr_type_p (main_odr_variant (type)))
2042 get_odr_type (type, true);
2045 /* Return true if type is known to have no derivations. */
2047 bool
2048 type_known_to_have_no_deriavations_p (tree t)
2050 return (type_all_derivations_known_p (t)
2051 && (TYPE_FINAL_P (t)
2052 || (odr_hash
2053 && !get_odr_type (t, true)->derived_types.length())));
2056 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2057 recursive printing. */
2059 static void
2060 dump_odr_type (FILE *f, odr_type t, int indent=0)
2062 unsigned int i;
2063 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2064 print_generic_expr (f, t->type, TDF_SLIM);
2065 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2066 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2067 if (TYPE_NAME (t->type))
2069 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2070 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2071 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2072 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2073 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2074 IDENTIFIER_POINTER
2075 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2077 if (t->bases.length ())
2079 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2080 for (i = 0; i < t->bases.length (); i++)
2081 fprintf (f, " %i", t->bases[i]->id);
2082 fprintf (f, "\n");
2084 if (t->derived_types.length ())
2086 fprintf (f, "%*s derived types:\n", indent * 2, "");
2087 for (i = 0; i < t->derived_types.length (); i++)
2088 dump_odr_type (f, t->derived_types[i], indent + 1);
2090 fprintf (f, "\n");
2093 /* Dump the type inheritance graph. */
2095 static void
2096 dump_type_inheritance_graph (FILE *f)
2098 unsigned int i;
2099 if (!odr_types_ptr)
2100 return;
2101 fprintf (f, "\n\nType inheritance graph:\n");
2102 for (i = 0; i < odr_types.length (); i++)
2104 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2105 dump_odr_type (f, odr_types[i]);
2107 for (i = 0; i < odr_types.length (); i++)
2109 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
2111 unsigned int j;
2112 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2113 print_node (f, "", odr_types[i]->type, 0);
2114 for (j = 0; j < odr_types[i]->types->length (); j++)
2116 tree t;
2117 fprintf (f, "duplicate #%i\n", j);
2118 print_node (f, "", (*odr_types[i]->types)[j], 0);
2119 t = (*odr_types[i]->types)[j];
2120 while (TYPE_P (t) && TYPE_CONTEXT (t))
2122 t = TYPE_CONTEXT (t);
2123 print_node (f, "", t, 0);
2125 putc ('\n',f);
2131 /* Given method type T, return type of class it belongs to.
2132 Look up this pointer and get its type. */
2134 tree
2135 method_class_type (const_tree t)
2137 tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
2138 gcc_assert (TREE_CODE (t) == METHOD_TYPE);
2140 return TREE_TYPE (first_parm_type);
2143 /* Initialize IPA devirt and build inheritance tree graph. */
2145 void
2146 build_type_inheritance_graph (void)
2148 struct symtab_node *n;
2149 FILE *inheritance_dump_file;
2150 int flags;
2152 if (odr_hash)
2153 return;
2154 timevar_push (TV_IPA_INHERITANCE);
2155 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2156 odr_hash = new odr_hash_type (23);
2157 if (in_lto_p)
2158 odr_vtable_hash = new odr_vtable_hash_type (23);
2160 /* We reconstruct the graph starting of types of all methods seen in the
2161 the unit. */
2162 FOR_EACH_SYMBOL (n)
2163 if (is_a <cgraph_node *> (n)
2164 && DECL_VIRTUAL_P (n->decl)
2165 && n->real_symbol_p ())
2166 get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n->decl))),
2167 true);
2169 /* Look also for virtual tables of types that do not define any methods.
2171 We need it in a case where class B has virtual base of class A
2172 re-defining its virtual method and there is class C with no virtual
2173 methods with B as virtual base.
2175 Here we output B's virtual method in two variant - for non-virtual
2176 and virtual inheritance. B's virtual table has non-virtual version,
2177 while C's has virtual.
2179 For this reason we need to know about C in order to include both
2180 variants of B. More correctly, record_target_from_binfo should
2181 add both variants of the method when walking B, but we have no
2182 link in between them.
2184 We rely on fact that either the method is exported and thus we
2185 assume it is called externally or C is in anonymous namespace and
2186 thus we will see the vtable. */
2188 else if (is_a <varpool_node *> (n)
2189 && DECL_VIRTUAL_P (n->decl)
2190 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2191 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2192 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2193 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2194 if (inheritance_dump_file)
2196 dump_type_inheritance_graph (inheritance_dump_file);
2197 dump_end (TDI_inheritance, inheritance_dump_file);
2199 timevar_pop (TV_IPA_INHERITANCE);
2202 /* Return true if N has reference from live virtual table
2203 (and thus can be a destination of polymorphic call).
2204 Be conservatively correct when callgraph is not built or
2205 if the method may be referred externally. */
2207 static bool
2208 referenced_from_vtable_p (struct cgraph_node *node)
2210 int i;
2211 struct ipa_ref *ref;
2212 bool found = false;
2214 if (node->externally_visible
2215 || DECL_EXTERNAL (node->decl)
2216 || node->used_from_other_partition)
2217 return true;
2219 /* Keep this test constant time.
2220 It is unlikely this can happen except for the case where speculative
2221 devirtualization introduced many speculative edges to this node.
2222 In this case the target is very likely alive anyway. */
2223 if (node->ref_list.referring.length () > 100)
2224 return true;
2226 /* We need references built. */
2227 if (symtab->state <= CONSTRUCTION)
2228 return true;
2230 for (i = 0; node->iterate_referring (i, ref); i++)
2231 if ((ref->use == IPA_REF_ALIAS
2232 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2233 || (ref->use == IPA_REF_ADDR
2234 && TREE_CODE (ref->referring->decl) == VAR_DECL
2235 && DECL_VIRTUAL_P (ref->referring->decl)))
2237 found = true;
2238 break;
2240 return found;
2243 /* If TARGET has associated node, record it in the NODES array.
2244 CAN_REFER specify if program can refer to the target directly.
2245 if TARGET is unknown (NULL) or it can not be inserted (for example because
2246 its body was already removed and there is no way to refer to it), clear
2247 COMPLETEP. */
2249 static void
2250 maybe_record_node (vec <cgraph_node *> &nodes,
2251 tree target, hash_set<tree> *inserted,
2252 bool can_refer,
2253 bool *completep)
2255 struct cgraph_node *target_node, *alias_target;
2256 enum availability avail;
2258 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
2259 list of targets; the runtime effect of calling them is undefined.
2260 Only "real" virtual methods should be accounted. */
2261 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
2262 return;
2264 if (!can_refer)
2266 /* The only case when method of anonymous namespace becomes unreferable
2267 is when we completely optimized it out. */
2268 if (flag_ltrans
2269 || !target
2270 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2271 *completep = false;
2272 return;
2275 if (!target)
2276 return;
2278 target_node = cgraph_node::get (target);
2280 /* Prefer alias target over aliases, so we do not get confused by
2281 fake duplicates. */
2282 if (target_node)
2284 alias_target = target_node->ultimate_alias_target (&avail);
2285 if (target_node != alias_target
2286 && avail >= AVAIL_AVAILABLE
2287 && target_node->get_availability ())
2288 target_node = alias_target;
2291 /* Method can only be called by polymorphic call if any
2292 of vtables referring to it are alive.
2294 While this holds for non-anonymous functions, too, there are
2295 cases where we want to keep them in the list; for example
2296 inline functions with -fno-weak are static, but we still
2297 may devirtualize them when instance comes from other unit.
2298 The same holds for LTO.
2300 Currently we ignore these functions in speculative devirtualization.
2301 ??? Maybe it would make sense to be more aggressive for LTO even
2302 elsewhere. */
2303 if (!flag_ltrans
2304 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2305 && (!target_node
2306 || !referenced_from_vtable_p (target_node)))
2308 /* See if TARGET is useful function we can deal with. */
2309 else if (target_node != NULL
2310 && (TREE_PUBLIC (target)
2311 || DECL_EXTERNAL (target)
2312 || target_node->definition)
2313 && target_node->real_symbol_p ())
2315 gcc_assert (!target_node->global.inlined_to);
2316 gcc_assert (target_node->real_symbol_p ());
2317 if (!inserted->add (target))
2319 cached_polymorphic_call_targets->add (target_node);
2320 nodes.safe_push (target_node);
2323 else if (completep
2324 && (!type_in_anonymous_namespace_p
2325 (DECL_CONTEXT (target))
2326 || flag_ltrans))
2327 *completep = false;
2330 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2331 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2332 method in vtable and insert method to NODES array
2333 or BASES_TO_CONSIDER if this array is non-NULL.
2334 Otherwise recurse to base BINFOs.
2335 This matches what get_binfo_at_offset does, but with offset
2336 being unknown.
2338 TYPE_BINFOS is a stack of BINFOS of types with defined
2339 virtual table seen on way from class type to BINFO.
2341 MATCHED_VTABLES tracks virtual tables we already did lookup
2342 for virtual function in. INSERTED tracks nodes we already
2343 inserted.
2345 ANONYMOUS is true if BINFO is part of anonymous namespace.
2347 Clear COMPLETEP when we hit unreferable target.
2350 static void
2351 record_target_from_binfo (vec <cgraph_node *> &nodes,
2352 vec <tree> *bases_to_consider,
2353 tree binfo,
2354 tree otr_type,
2355 vec <tree> &type_binfos,
2356 HOST_WIDE_INT otr_token,
2357 tree outer_type,
2358 HOST_WIDE_INT offset,
2359 hash_set<tree> *inserted,
2360 hash_set<tree> *matched_vtables,
2361 bool anonymous,
2362 bool *completep)
2364 tree type = BINFO_TYPE (binfo);
2365 int i;
2366 tree base_binfo;
2369 if (BINFO_VTABLE (binfo))
2370 type_binfos.safe_push (binfo);
2371 if (types_same_for_odr (type, outer_type))
2373 int i;
2374 tree type_binfo = NULL;
2376 /* Look up BINFO with virtual table. For normal types it is always last
2377 binfo on stack. */
2378 for (i = type_binfos.length () - 1; i >= 0; i--)
2379 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2381 type_binfo = type_binfos[i];
2382 break;
2384 if (BINFO_VTABLE (binfo))
2385 type_binfos.pop ();
2386 /* If this is duplicated BINFO for base shared by virtual inheritance,
2387 we may not have its associated vtable. This is not a problem, since
2388 we will walk it on the other path. */
2389 if (!type_binfo)
2390 return;
2391 tree inner_binfo = get_binfo_at_offset (type_binfo,
2392 offset, otr_type);
2393 if (!inner_binfo)
2395 gcc_assert (odr_violation_reported);
2396 return;
2398 /* For types in anonymous namespace first check if the respective vtable
2399 is alive. If not, we know the type can't be called. */
2400 if (!flag_ltrans && anonymous)
2402 tree vtable = BINFO_VTABLE (inner_binfo);
2403 varpool_node *vnode;
2405 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2406 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2407 vnode = varpool_node::get (vtable);
2408 if (!vnode || !vnode->definition)
2409 return;
2411 gcc_assert (inner_binfo);
2412 if (bases_to_consider
2413 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2414 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2416 bool can_refer;
2417 tree target = gimple_get_virt_method_for_binfo (otr_token,
2418 inner_binfo,
2419 &can_refer);
2420 if (!bases_to_consider)
2421 maybe_record_node (nodes, target, inserted, can_refer, completep);
2422 /* Destructors are never called via construction vtables. */
2423 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2424 bases_to_consider->safe_push (target);
2426 return;
2429 /* Walk bases. */
2430 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2431 /* Walking bases that have no virtual method is pointless exercise. */
2432 if (polymorphic_type_binfo_p (base_binfo))
2433 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2434 type_binfos,
2435 otr_token, outer_type, offset, inserted,
2436 matched_vtables, anonymous, completep);
2437 if (BINFO_VTABLE (binfo))
2438 type_binfos.pop ();
2441 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2442 of TYPE, insert them to NODES, recurse into derived nodes.
2443 INSERTED is used to avoid duplicate insertions of methods into NODES.
2444 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2445 Clear COMPLETEP if unreferable target is found.
2447 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2448 all cases where BASE_SKIPPED is true (because the base is abstract
2449 class). */
2451 static void
2452 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2453 hash_set<tree> *inserted,
2454 hash_set<tree> *matched_vtables,
2455 tree otr_type,
2456 odr_type type,
2457 HOST_WIDE_INT otr_token,
2458 tree outer_type,
2459 HOST_WIDE_INT offset,
2460 bool *completep,
2461 vec <tree> &bases_to_consider,
2462 bool consider_construction)
2464 tree binfo = TYPE_BINFO (type->type);
2465 unsigned int i;
2466 auto_vec <tree, 8> type_binfos;
2467 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2469 /* We may need to consider types w/o instances because of possible derived
2470 types using their methods either directly or via construction vtables.
2471 We are safe to skip them when all derivations are known, since we will
2472 handle them later.
2473 This is done by recording them to BASES_TO_CONSIDER array. */
2474 if (possibly_instantiated || consider_construction)
2476 record_target_from_binfo (nodes,
2477 (!possibly_instantiated
2478 && type_all_derivations_known_p (type->type))
2479 ? &bases_to_consider : NULL,
2480 binfo, otr_type, type_binfos, otr_token,
2481 outer_type, offset,
2482 inserted, matched_vtables,
2483 type->anonymous_namespace, completep);
2485 for (i = 0; i < type->derived_types.length (); i++)
2486 possible_polymorphic_call_targets_1 (nodes, inserted,
2487 matched_vtables,
2488 otr_type,
2489 type->derived_types[i],
2490 otr_token, outer_type, offset, completep,
2491 bases_to_consider, consider_construction);
2494 /* Cache of queries for polymorphic call targets.
2496 Enumerating all call targets may get expensive when there are many
2497 polymorphic calls in the program, so we memoize all the previous
2498 queries and avoid duplicated work. */
2500 struct polymorphic_call_target_d
2502 HOST_WIDE_INT otr_token;
2503 ipa_polymorphic_call_context context;
2504 odr_type type;
2505 vec <cgraph_node *> targets;
2506 tree decl_warning;
2507 int type_warning;
2508 bool complete;
2509 bool speculative;
2512 /* Polymorphic call target cache helpers. */
2514 struct polymorphic_call_target_hasher
2516 typedef polymorphic_call_target_d *value_type;
2517 typedef polymorphic_call_target_d *compare_type;
2518 static inline hashval_t hash (const polymorphic_call_target_d *);
2519 static inline bool equal (const polymorphic_call_target_d *,
2520 const polymorphic_call_target_d *);
2521 static inline void remove (polymorphic_call_target_d *);
2524 /* Return the computed hashcode for ODR_QUERY. */
2526 inline hashval_t
2527 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2529 inchash::hash hstate (odr_query->otr_token);
2531 hstate.add_wide_int (odr_query->type->id);
2532 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2533 hstate.add_wide_int (odr_query->context.offset);
2535 if (odr_query->context.speculative_outer_type)
2537 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2538 hstate.add_wide_int (odr_query->context.speculative_offset);
2540 hstate.add_flag (odr_query->speculative);
2541 hstate.add_flag (odr_query->context.maybe_in_construction);
2542 hstate.add_flag (odr_query->context.maybe_derived_type);
2543 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2544 hstate.commit_flag ();
2545 return hstate.end ();
2548 /* Compare cache entries T1 and T2. */
2550 inline bool
2551 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2552 const polymorphic_call_target_d *t2)
2554 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2555 && t1->speculative == t2->speculative
2556 && t1->context.offset == t2->context.offset
2557 && t1->context.speculative_offset == t2->context.speculative_offset
2558 && t1->context.outer_type == t2->context.outer_type
2559 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2560 && t1->context.maybe_in_construction
2561 == t2->context.maybe_in_construction
2562 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2563 && (t1->context.speculative_maybe_derived_type
2564 == t2->context.speculative_maybe_derived_type));
2567 /* Remove entry in polymorphic call target cache hash. */
2569 inline void
2570 polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2572 v->targets.release ();
2573 free (v);
2576 /* Polymorphic call target query cache. */
2578 typedef hash_table<polymorphic_call_target_hasher>
2579 polymorphic_call_target_hash_type;
2580 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2582 /* Destroy polymorphic call target query cache. */
2584 static void
2585 free_polymorphic_call_targets_hash ()
2587 if (cached_polymorphic_call_targets)
2589 delete polymorphic_call_target_hash;
2590 polymorphic_call_target_hash = NULL;
2591 delete cached_polymorphic_call_targets;
2592 cached_polymorphic_call_targets = NULL;
2596 /* When virtual function is removed, we may need to flush the cache. */
2598 static void
2599 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2601 if (cached_polymorphic_call_targets
2602 && cached_polymorphic_call_targets->contains (n))
2603 free_polymorphic_call_targets_hash ();
2606 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2608 tree
2609 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2610 tree vtable)
2612 tree v = BINFO_VTABLE (binfo);
2613 int i;
2614 tree base_binfo;
2615 unsigned HOST_WIDE_INT this_offset;
2617 if (v)
2619 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2620 gcc_unreachable ();
2622 if (offset == this_offset
2623 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2624 return binfo;
2627 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2628 if (polymorphic_type_binfo_p (base_binfo))
2630 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2631 if (base_binfo)
2632 return base_binfo;
2634 return NULL;
2637 /* T is known constant value of virtual table pointer.
2638 Store virtual table to V and its offset to OFFSET.
2639 Return false if T does not look like virtual table reference. */
2641 bool
2642 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2643 unsigned HOST_WIDE_INT *offset)
2645 /* We expect &MEM[(void *)&virtual_table + 16B].
2646 We obtain object's BINFO from the context of the virtual table.
2647 This one contains pointer to virtual table represented via
2648 POINTER_PLUS_EXPR. Verify that this pointer matches what
2649 we propagated through.
2651 In the case of virtual inheritance, the virtual tables may
2652 be nested, i.e. the offset may be different from 16 and we may
2653 need to dive into the type representation. */
2654 if (TREE_CODE (t) == ADDR_EXPR
2655 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2656 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2657 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2658 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2659 == VAR_DECL)
2660 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2661 (TREE_OPERAND (t, 0), 0), 0)))
2663 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2664 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2665 return true;
2668 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2669 We need to handle it when T comes from static variable initializer or
2670 BINFO. */
2671 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2673 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2674 t = TREE_OPERAND (t, 0);
2676 else
2677 *offset = 0;
2679 if (TREE_CODE (t) != ADDR_EXPR)
2680 return false;
2681 *v = TREE_OPERAND (t, 0);
2682 return true;
2685 /* T is known constant value of virtual table pointer. Return BINFO of the
2686 instance type. */
2688 tree
2689 vtable_pointer_value_to_binfo (const_tree t)
2691 tree vtable;
2692 unsigned HOST_WIDE_INT offset;
2694 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2695 return NULL_TREE;
2697 /* FIXME: for stores of construction vtables we return NULL,
2698 because we do not have BINFO for those. Eventually we should fix
2699 our representation to allow this case to be handled, too.
2700 In the case we see store of BINFO we however may assume
2701 that standard folding will be able to cope with it. */
2702 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2703 offset, vtable);
2706 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2707 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2708 and insert them in NODES.
2710 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2712 static void
2713 record_targets_from_bases (tree otr_type,
2714 HOST_WIDE_INT otr_token,
2715 tree outer_type,
2716 HOST_WIDE_INT offset,
2717 vec <cgraph_node *> &nodes,
2718 hash_set<tree> *inserted,
2719 hash_set<tree> *matched_vtables,
2720 bool *completep)
2722 while (true)
2724 HOST_WIDE_INT pos, size;
2725 tree base_binfo;
2726 tree fld;
2728 if (types_same_for_odr (outer_type, otr_type))
2729 return;
2731 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2733 if (TREE_CODE (fld) != FIELD_DECL)
2734 continue;
2736 pos = int_bit_position (fld);
2737 size = tree_to_shwi (DECL_SIZE (fld));
2738 if (pos <= offset && (pos + size) > offset
2739 /* Do not get confused by zero sized bases. */
2740 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2741 break;
2743 /* Within a class type we should always find corresponding fields. */
2744 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2746 /* Nonbase types should have been stripped by outer_class_type. */
2747 gcc_assert (DECL_ARTIFICIAL (fld));
2749 outer_type = TREE_TYPE (fld);
2750 offset -= pos;
2752 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2753 offset, otr_type);
2754 if (!base_binfo)
2756 gcc_assert (odr_violation_reported);
2757 return;
2759 gcc_assert (base_binfo);
2760 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2762 bool can_refer;
2763 tree target = gimple_get_virt_method_for_binfo (otr_token,
2764 base_binfo,
2765 &can_refer);
2766 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2767 maybe_record_node (nodes, target, inserted, can_refer, completep);
2768 matched_vtables->add (BINFO_VTABLE (base_binfo));
2773 /* When virtual table is removed, we may need to flush the cache. */
2775 static void
2776 devirt_variable_node_removal_hook (varpool_node *n,
2777 void *d ATTRIBUTE_UNUSED)
2779 if (cached_polymorphic_call_targets
2780 && DECL_VIRTUAL_P (n->decl)
2781 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2782 free_polymorphic_call_targets_hash ();
2785 /* Record about how many calls would benefit from given type to be final. */
2787 struct odr_type_warn_count
2789 tree type;
2790 int count;
2791 gcov_type dyn_count;
2794 /* Record about how many calls would benefit from given method to be final. */
2796 struct decl_warn_count
2798 tree decl;
2799 int count;
2800 gcov_type dyn_count;
2803 /* Information about type and decl warnings. */
2805 struct final_warning_record
2807 gcov_type dyn_count;
2808 vec<odr_type_warn_count> type_warnings;
2809 hash_map<tree, decl_warn_count> decl_warnings;
2811 struct final_warning_record *final_warning_records;
2813 /* Return vector containing possible targets of polymorphic call of type
2814 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2815 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2816 OTR_TYPE and include their virtual method. This is useful for types
2817 possibly in construction or destruction where the virtual table may
2818 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2819 us to walk the inheritance graph for all derivations.
2821 If COMPLETEP is non-NULL, store true if the list is complete.
2822 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2823 in the target cache. If user needs to visit every target list
2824 just once, it can memoize them.
2826 If SPECULATIVE is set, the list will not contain targets that
2827 are not speculatively taken.
2829 Returned vector is placed into cache. It is NOT caller's responsibility
2830 to free it. The vector can be freed on cgraph_remove_node call if
2831 the particular node is a virtual function present in the cache. */
2833 vec <cgraph_node *>
2834 possible_polymorphic_call_targets (tree otr_type,
2835 HOST_WIDE_INT otr_token,
2836 ipa_polymorphic_call_context context,
2837 bool *completep,
2838 void **cache_token,
2839 bool speculative)
2841 static struct cgraph_node_hook_list *node_removal_hook_holder;
2842 vec <cgraph_node *> nodes = vNULL;
2843 auto_vec <tree, 8> bases_to_consider;
2844 odr_type type, outer_type;
2845 polymorphic_call_target_d key;
2846 polymorphic_call_target_d **slot;
2847 unsigned int i;
2848 tree binfo, target;
2849 bool complete;
2850 bool can_refer = false;
2851 bool skipped = false;
2853 otr_type = TYPE_MAIN_VARIANT (otr_type);
2855 /* If ODR is not initialized or the context is invalid, return empty
2856 incomplete list. */
2857 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
2859 if (completep)
2860 *completep = context.invalid;
2861 if (cache_token)
2862 *cache_token = NULL;
2863 return nodes;
2866 /* Do not bother to compute speculative info when user do not asks for it. */
2867 if (!speculative || !context.speculative_outer_type)
2868 context.clear_speculation ();
2870 type = get_odr_type (otr_type, true);
2872 /* Recording type variants would waste results cache. */
2873 gcc_assert (!context.outer_type
2874 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2876 /* Look up the outer class type we want to walk.
2877 If we fail to do so, the context is invalid. */
2878 if ((context.outer_type || context.speculative_outer_type)
2879 && !context.restrict_to_inner_class (otr_type))
2881 if (completep)
2882 *completep = true;
2883 if (cache_token)
2884 *cache_token = NULL;
2885 return nodes;
2887 gcc_assert (!context.invalid);
2889 /* Check that restrict_to_inner_class kept the main variant. */
2890 gcc_assert (!context.outer_type
2891 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2893 /* We canonicalize our query, so we do not need extra hashtable entries. */
2895 /* Without outer type, we have no use for offset. Just do the
2896 basic search from inner type. */
2897 if (!context.outer_type)
2898 context.clear_outer_type (otr_type);
2899 /* We need to update our hierarchy if the type does not exist. */
2900 outer_type = get_odr_type (context.outer_type, true);
2901 /* If the type is complete, there are no derivations. */
2902 if (TYPE_FINAL_P (outer_type->type))
2903 context.maybe_derived_type = false;
2905 /* Initialize query cache. */
2906 if (!cached_polymorphic_call_targets)
2908 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
2909 polymorphic_call_target_hash
2910 = new polymorphic_call_target_hash_type (23);
2911 if (!node_removal_hook_holder)
2913 node_removal_hook_holder =
2914 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
2915 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
2916 NULL);
2920 if (in_lto_p)
2922 if (context.outer_type != otr_type)
2923 context.outer_type
2924 = get_odr_type (context.outer_type, true)->type;
2925 if (context.speculative_outer_type)
2926 context.speculative_outer_type
2927 = get_odr_type (context.speculative_outer_type, true)->type;
2930 /* Look up cached answer. */
2931 key.type = type;
2932 key.otr_token = otr_token;
2933 key.speculative = speculative;
2934 key.context = context;
2935 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
2936 if (cache_token)
2937 *cache_token = (void *)*slot;
2938 if (*slot)
2940 if (completep)
2941 *completep = (*slot)->complete;
2942 if ((*slot)->type_warning && final_warning_records)
2944 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
2945 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
2946 += final_warning_records->dyn_count;
2948 if (!speculative && (*slot)->decl_warning && final_warning_records)
2950 struct decl_warn_count *c =
2951 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
2952 c->count++;
2953 c->dyn_count += final_warning_records->dyn_count;
2955 return (*slot)->targets;
2958 complete = true;
2960 /* Do actual search. */
2961 timevar_push (TV_IPA_VIRTUAL_CALL);
2962 *slot = XCNEW (polymorphic_call_target_d);
2963 if (cache_token)
2964 *cache_token = (void *)*slot;
2965 (*slot)->type = type;
2966 (*slot)->otr_token = otr_token;
2967 (*slot)->context = context;
2968 (*slot)->speculative = speculative;
2970 hash_set<tree> inserted;
2971 hash_set<tree> matched_vtables;
2973 /* First insert targets we speculatively identified as likely. */
2974 if (context.speculative_outer_type)
2976 odr_type speculative_outer_type;
2977 bool speculation_complete = true;
2979 /* First insert target from type itself and check if it may have
2980 derived types. */
2981 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
2982 if (TYPE_FINAL_P (speculative_outer_type->type))
2983 context.speculative_maybe_derived_type = false;
2984 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
2985 context.speculative_offset, otr_type);
2986 if (binfo)
2987 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
2988 &can_refer);
2989 else
2990 target = NULL;
2992 /* In the case we get complete method, we don't need
2993 to walk derivations. */
2994 if (target && DECL_FINAL_P (target))
2995 context.speculative_maybe_derived_type = false;
2996 if (type_possibly_instantiated_p (speculative_outer_type->type))
2997 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
2998 if (binfo)
2999 matched_vtables.add (BINFO_VTABLE (binfo));
3002 /* Next walk recursively all derived types. */
3003 if (context.speculative_maybe_derived_type)
3004 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3005 possible_polymorphic_call_targets_1 (nodes, &inserted,
3006 &matched_vtables,
3007 otr_type,
3008 speculative_outer_type->derived_types[i],
3009 otr_token, speculative_outer_type->type,
3010 context.speculative_offset,
3011 &speculation_complete,
3012 bases_to_consider,
3013 false);
3016 if (!speculative || !nodes.length ())
3018 /* First see virtual method of type itself. */
3019 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3020 context.offset, otr_type);
3021 if (binfo)
3022 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3023 &can_refer);
3024 else
3026 gcc_assert (odr_violation_reported);
3027 target = NULL;
3030 /* Destructors are never called through construction virtual tables,
3031 because the type is always known. */
3032 if (target && DECL_CXX_DESTRUCTOR_P (target))
3033 context.maybe_in_construction = false;
3035 if (target)
3037 /* In the case we get complete method, we don't need
3038 to walk derivations. */
3039 if (DECL_FINAL_P (target))
3040 context.maybe_derived_type = false;
3043 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3044 if (type_possibly_instantiated_p (outer_type->type))
3045 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3046 else
3047 skipped = true;
3049 if (binfo)
3050 matched_vtables.add (BINFO_VTABLE (binfo));
3052 /* Next walk recursively all derived types. */
3053 if (context.maybe_derived_type)
3055 for (i = 0; i < outer_type->derived_types.length(); i++)
3056 possible_polymorphic_call_targets_1 (nodes, &inserted,
3057 &matched_vtables,
3058 otr_type,
3059 outer_type->derived_types[i],
3060 otr_token, outer_type->type,
3061 context.offset, &complete,
3062 bases_to_consider,
3063 context.maybe_in_construction);
3065 if (!outer_type->all_derivations_known)
3067 if (!speculative && final_warning_records)
3069 if (complete
3070 && nodes.length () == 1
3071 && warn_suggest_final_types
3072 && !outer_type->derived_types.length ())
3074 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
3075 final_warning_records->type_warnings.safe_grow_cleared
3076 (odr_types.length ());
3077 final_warning_records->type_warnings[outer_type->id].count++;
3078 final_warning_records->type_warnings[outer_type->id].dyn_count
3079 += final_warning_records->dyn_count;
3080 final_warning_records->type_warnings[outer_type->id].type
3081 = outer_type->type;
3082 (*slot)->type_warning = outer_type->id + 1;
3084 if (complete
3085 && warn_suggest_final_methods
3086 && nodes.length () == 1
3087 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3088 outer_type->type))
3090 bool existed;
3091 struct decl_warn_count &c =
3092 final_warning_records->decl_warnings.get_or_insert
3093 (nodes[0]->decl, &existed);
3095 if (existed)
3097 c.count++;
3098 c.dyn_count += final_warning_records->dyn_count;
3100 else
3102 c.count = 1;
3103 c.dyn_count = final_warning_records->dyn_count;
3104 c.decl = nodes[0]->decl;
3106 (*slot)->decl_warning = nodes[0]->decl;
3109 complete = false;
3113 if (!speculative)
3115 /* Destructors are never called through construction virtual tables,
3116 because the type is always known. One of entries may be
3117 cxa_pure_virtual so look to at least two of them. */
3118 if (context.maybe_in_construction)
3119 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3120 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3121 context.maybe_in_construction = false;
3122 if (context.maybe_in_construction)
3124 if (type != outer_type
3125 && (!skipped
3126 || (context.maybe_derived_type
3127 && !type_all_derivations_known_p (outer_type->type))))
3128 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3129 context.offset, nodes, &inserted,
3130 &matched_vtables, &complete);
3131 if (skipped)
3132 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3133 for (i = 0; i < bases_to_consider.length(); i++)
3134 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3139 (*slot)->targets = nodes;
3140 (*slot)->complete = complete;
3141 if (completep)
3142 *completep = complete;
3144 timevar_pop (TV_IPA_VIRTUAL_CALL);
3145 return nodes;
3148 bool
3149 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3150 vec<const decl_warn_count*> *vec)
3152 vec->safe_push (&value);
3153 return true;
3156 /* Dump target list TARGETS into FILE. */
3158 static void
3159 dump_targets (FILE *f, vec <cgraph_node *> targets)
3161 unsigned int i;
3163 for (i = 0; i < targets.length (); i++)
3165 char *name = NULL;
3166 if (in_lto_p)
3167 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3168 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
3169 if (in_lto_p)
3170 free (name);
3171 if (!targets[i]->definition)
3172 fprintf (f, " (no definition%s)",
3173 DECL_DECLARED_INLINE_P (targets[i]->decl)
3174 ? " inline" : "");
3176 fprintf (f, "\n");
3179 /* Dump all possible targets of a polymorphic call. */
3181 void
3182 dump_possible_polymorphic_call_targets (FILE *f,
3183 tree otr_type,
3184 HOST_WIDE_INT otr_token,
3185 const ipa_polymorphic_call_context &ctx)
3187 vec <cgraph_node *> targets;
3188 bool final;
3189 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3190 unsigned int len;
3192 if (!type)
3193 return;
3194 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3195 ctx,
3196 &final, NULL, false);
3197 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3198 print_generic_expr (f, type->type, TDF_SLIM);
3199 fprintf (f, " token %i\n", (int)otr_token);
3201 ctx.dump (f);
3203 fprintf (f, " %s%s%s%s\n ",
3204 final ? "This is a complete list." :
3205 "This is partial list; extra targets may be defined in other units.",
3206 ctx.maybe_in_construction ? " (base types included)" : "",
3207 ctx.maybe_derived_type ? " (derived types included)" : "",
3208 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3209 len = targets.length ();
3210 dump_targets (f, targets);
3212 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3213 ctx,
3214 &final, NULL, true);
3215 if (targets.length () != len)
3217 fprintf (f, " Speculative targets:");
3218 dump_targets (f, targets);
3220 gcc_assert (targets.length () <= len);
3221 fprintf (f, "\n");
3225 /* Return true if N can be possibly target of a polymorphic call of
3226 OTR_TYPE/OTR_TOKEN. */
3228 bool
3229 possible_polymorphic_call_target_p (tree otr_type,
3230 HOST_WIDE_INT otr_token,
3231 const ipa_polymorphic_call_context &ctx,
3232 struct cgraph_node *n)
3234 vec <cgraph_node *> targets;
3235 unsigned int i;
3236 enum built_in_function fcode;
3237 bool final;
3239 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3240 && ((fcode = DECL_FUNCTION_CODE (n->decl))
3241 == BUILT_IN_UNREACHABLE
3242 || fcode == BUILT_IN_TRAP))
3243 return true;
3245 if (!odr_hash)
3246 return true;
3247 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3248 for (i = 0; i < targets.length (); i++)
3249 if (n->semantically_equivalent_p (targets[i]))
3250 return true;
3252 /* At a moment we allow middle end to dig out new external declarations
3253 as a targets of polymorphic calls. */
3254 if (!final && !n->definition)
3255 return true;
3256 return false;
3261 /* Return true if N can be possibly target of a polymorphic call of
3262 OBJ_TYPE_REF expression REF in STMT. */
3264 bool
3265 possible_polymorphic_call_target_p (tree ref,
3266 gimple stmt,
3267 struct cgraph_node *n)
3269 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3270 tree call_fn = gimple_call_fn (stmt);
3272 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3273 tree_to_uhwi
3274 (OBJ_TYPE_REF_TOKEN (call_fn)),
3275 context,
3280 /* After callgraph construction new external nodes may appear.
3281 Add them into the graph. */
3283 void
3284 update_type_inheritance_graph (void)
3286 struct cgraph_node *n;
3288 if (!odr_hash)
3289 return;
3290 free_polymorphic_call_targets_hash ();
3291 timevar_push (TV_IPA_INHERITANCE);
3292 /* We reconstruct the graph starting from types of all methods seen in the
3293 the unit. */
3294 FOR_EACH_FUNCTION (n)
3295 if (DECL_VIRTUAL_P (n->decl)
3296 && !n->definition
3297 && n->real_symbol_p ())
3298 get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n->decl))),
3299 true);
3300 timevar_pop (TV_IPA_INHERITANCE);
3304 /* Return true if N looks like likely target of a polymorphic call.
3305 Rule out cxa_pure_virtual, noreturns, function declared cold and
3306 other obvious cases. */
3308 bool
3309 likely_target_p (struct cgraph_node *n)
3311 int flags;
3312 /* cxa_pure_virtual and similar things are not likely. */
3313 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3314 return false;
3315 flags = flags_from_decl_or_type (n->decl);
3316 if (flags & ECF_NORETURN)
3317 return false;
3318 if (lookup_attribute ("cold",
3319 DECL_ATTRIBUTES (n->decl)))
3320 return false;
3321 if (n->frequency < NODE_FREQUENCY_NORMAL)
3322 return false;
3323 /* If there are no live virtual tables referring the target,
3324 the only way the target can be called is an instance coming from other
3325 compilation unit; speculative devirtualization is built around an
3326 assumption that won't happen. */
3327 if (!referenced_from_vtable_p (n))
3328 return false;
3329 return true;
3332 /* Compare type warning records P1 and P2 and choose one with larger count;
3333 helper for qsort. */
3336 type_warning_cmp (const void *p1, const void *p2)
3338 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3339 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3341 if (t1->dyn_count < t2->dyn_count)
3342 return 1;
3343 if (t1->dyn_count > t2->dyn_count)
3344 return -1;
3345 return t2->count - t1->count;
3348 /* Compare decl warning records P1 and P2 and choose one with larger count;
3349 helper for qsort. */
3352 decl_warning_cmp (const void *p1, const void *p2)
3354 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3355 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3357 if (t1->dyn_count < t2->dyn_count)
3358 return 1;
3359 if (t1->dyn_count > t2->dyn_count)
3360 return -1;
3361 return t2->count - t1->count;
3365 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3366 context CTX. */
3368 struct cgraph_node *
3369 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3370 ipa_polymorphic_call_context ctx)
3372 vec <cgraph_node *>targets
3373 = possible_polymorphic_call_targets
3374 (otr_type, otr_token, ctx, NULL, NULL, true);
3375 unsigned int i;
3376 struct cgraph_node *likely_target = NULL;
3378 for (i = 0; i < targets.length (); i++)
3379 if (likely_target_p (targets[i]))
3381 if (likely_target)
3382 return NULL;
3383 likely_target = targets[i];
3385 if (!likely_target
3386 ||!likely_target->definition
3387 || DECL_EXTERNAL (likely_target->decl))
3388 return NULL;
3390 /* Don't use an implicitly-declared destructor (c++/58678). */
3391 struct cgraph_node *non_thunk_target
3392 = likely_target->function_symbol ();
3393 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3394 return NULL;
3395 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3396 && likely_target->can_be_discarded_p ())
3397 return NULL;
3398 return likely_target;
3401 /* The ipa-devirt pass.
3402 When polymorphic call has only one likely target in the unit,
3403 turn it into a speculative call. */
3405 static unsigned int
3406 ipa_devirt (void)
3408 struct cgraph_node *n;
3409 hash_set<void *> bad_call_targets;
3410 struct cgraph_edge *e;
3412 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3413 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3414 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3415 int ndropped = 0;
3417 if (!odr_types_ptr)
3418 return 0;
3420 if (dump_file)
3421 dump_type_inheritance_graph (dump_file);
3423 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3424 This is implemented by setting up final_warning_records that are updated
3425 by get_polymorphic_call_targets.
3426 We need to clear cache in this case to trigger recomputation of all
3427 entries. */
3428 if (warn_suggest_final_methods || warn_suggest_final_types)
3430 final_warning_records = new (final_warning_record);
3431 final_warning_records->type_warnings = vNULL;
3432 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
3433 free_polymorphic_call_targets_hash ();
3436 FOR_EACH_DEFINED_FUNCTION (n)
3438 bool update = false;
3439 if (!opt_for_fn (n->decl, flag_devirtualize))
3440 continue;
3441 if (dump_file && n->indirect_calls)
3442 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
3443 n->name (), n->order);
3444 for (e = n->indirect_calls; e; e = e->next_callee)
3445 if (e->indirect_info->polymorphic)
3447 struct cgraph_node *likely_target = NULL;
3448 void *cache_token;
3449 bool final;
3451 if (final_warning_records)
3452 final_warning_records->dyn_count = e->count;
3454 vec <cgraph_node *>targets
3455 = possible_polymorphic_call_targets
3456 (e, &final, &cache_token, true);
3457 unsigned int i;
3459 /* Trigger warnings by calculating non-speculative targets. */
3460 if (warn_suggest_final_methods || warn_suggest_final_types)
3461 possible_polymorphic_call_targets (e);
3463 if (dump_file)
3464 dump_possible_polymorphic_call_targets
3465 (dump_file, e);
3467 npolymorphic++;
3469 /* See if the call can be devirtualized by means of ipa-prop's
3470 polymorphic call context propagation. If not, we can just
3471 forget about this call being polymorphic and avoid some heavy
3472 lifting in remove_unreachable_nodes that will otherwise try to
3473 keep all possible targets alive until inlining and in the inliner
3474 itself.
3476 This may need to be revisited once we add further ways to use
3477 the may edges, but it is a resonable thing to do right now. */
3479 if ((e->indirect_info->param_index == -1
3480 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3481 && e->indirect_info->vptr_changed))
3482 && !flag_ltrans_devirtualize)
3484 e->indirect_info->polymorphic = false;
3485 ndropped++;
3486 if (dump_file)
3487 fprintf (dump_file, "Dropping polymorphic call info;"
3488 " it can not be used by ipa-prop\n");
3491 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3492 continue;
3494 if (!e->maybe_hot_p ())
3496 if (dump_file)
3497 fprintf (dump_file, "Call is cold\n\n");
3498 ncold++;
3499 continue;
3501 if (e->speculative)
3503 if (dump_file)
3504 fprintf (dump_file, "Call is already speculated\n\n");
3505 nspeculated++;
3507 /* When dumping see if we agree with speculation. */
3508 if (!dump_file)
3509 continue;
3511 if (bad_call_targets.contains (cache_token))
3513 if (dump_file)
3514 fprintf (dump_file, "Target list is known to be useless\n\n");
3515 nmultiple++;
3516 continue;
3518 for (i = 0; i < targets.length (); i++)
3519 if (likely_target_p (targets[i]))
3521 if (likely_target)
3523 likely_target = NULL;
3524 if (dump_file)
3525 fprintf (dump_file, "More than one likely target\n\n");
3526 nmultiple++;
3527 break;
3529 likely_target = targets[i];
3531 if (!likely_target)
3533 bad_call_targets.add (cache_token);
3534 continue;
3536 /* This is reached only when dumping; check if we agree or disagree
3537 with the speculation. */
3538 if (e->speculative)
3540 struct cgraph_edge *e2;
3541 struct ipa_ref *ref;
3542 e->speculative_call_info (e2, e, ref);
3543 if (e2->callee->ultimate_alias_target ()
3544 == likely_target->ultimate_alias_target ())
3546 fprintf (dump_file, "We agree with speculation\n\n");
3547 nok++;
3549 else
3551 fprintf (dump_file, "We disagree with speculation\n\n");
3552 nwrong++;
3554 continue;
3556 if (!likely_target->definition)
3558 if (dump_file)
3559 fprintf (dump_file, "Target is not a definition\n\n");
3560 nnotdefined++;
3561 continue;
3563 /* Do not introduce new references to external symbols. While we
3564 can handle these just well, it is common for programs to
3565 incorrectly with headers defining methods they are linked
3566 with. */
3567 if (DECL_EXTERNAL (likely_target->decl))
3569 if (dump_file)
3570 fprintf (dump_file, "Target is external\n\n");
3571 nexternal++;
3572 continue;
3574 /* Don't use an implicitly-declared destructor (c++/58678). */
3575 struct cgraph_node *non_thunk_target
3576 = likely_target->function_symbol ();
3577 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3579 if (dump_file)
3580 fprintf (dump_file, "Target is artificial\n\n");
3581 nartificial++;
3582 continue;
3584 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3585 && likely_target->can_be_discarded_p ())
3587 if (dump_file)
3588 fprintf (dump_file, "Target is overwritable\n\n");
3589 noverwritable++;
3590 continue;
3592 else if (dbg_cnt (devirt))
3594 if (dump_enabled_p ())
3596 location_t locus = gimple_location_safe (e->call_stmt);
3597 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3598 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3599 n->name (), n->order,
3600 likely_target->name (),
3601 likely_target->order);
3603 if (!likely_target->can_be_discarded_p ())
3605 cgraph_node *alias;
3606 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3607 if (alias)
3608 likely_target = alias;
3610 nconverted++;
3611 update = true;
3612 e->make_speculative
3613 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
3616 if (update)
3617 inline_update_overall_summary (n);
3619 if (warn_suggest_final_methods || warn_suggest_final_types)
3621 if (warn_suggest_final_types)
3623 final_warning_records->type_warnings.qsort (type_warning_cmp);
3624 for (unsigned int i = 0;
3625 i < final_warning_records->type_warnings.length (); i++)
3626 if (final_warning_records->type_warnings[i].count)
3628 tree type = final_warning_records->type_warnings[i].type;
3629 int count = final_warning_records->type_warnings[i].count;
3630 long long dyn_count
3631 = final_warning_records->type_warnings[i].dyn_count;
3633 if (!dyn_count)
3634 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3635 OPT_Wsuggest_final_types, count,
3636 "Declaring type %qD final "
3637 "would enable devirtualization of %i call",
3638 "Declaring type %qD final "
3639 "would enable devirtualization of %i calls",
3640 type,
3641 count);
3642 else
3643 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3644 OPT_Wsuggest_final_types, count,
3645 "Declaring type %qD final "
3646 "would enable devirtualization of %i call "
3647 "executed %lli times",
3648 "Declaring type %qD final "
3649 "would enable devirtualization of %i calls "
3650 "executed %lli times",
3651 type,
3652 count,
3653 dyn_count);
3657 if (warn_suggest_final_methods)
3659 vec<const decl_warn_count*> decl_warnings_vec = vNULL;
3661 final_warning_records->decl_warnings.traverse
3662 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3663 decl_warnings_vec.qsort (decl_warning_cmp);
3664 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3666 tree decl = decl_warnings_vec[i]->decl;
3667 int count = decl_warnings_vec[i]->count;
3668 long long dyn_count = decl_warnings_vec[i]->dyn_count;
3670 if (!dyn_count)
3671 if (DECL_CXX_DESTRUCTOR_P (decl))
3672 warning_n (DECL_SOURCE_LOCATION (decl),
3673 OPT_Wsuggest_final_methods, count,
3674 "Declaring virtual destructor of %qD final "
3675 "would enable devirtualization of %i call",
3676 "Declaring virtual destructor of %qD final "
3677 "would enable devirtualization of %i calls",
3678 DECL_CONTEXT (decl), count);
3679 else
3680 warning_n (DECL_SOURCE_LOCATION (decl),
3681 OPT_Wsuggest_final_methods, count,
3682 "Declaring method %qD final "
3683 "would enable devirtualization of %i call",
3684 "Declaring method %qD final "
3685 "would enable devirtualization of %i calls",
3686 decl, count);
3687 else if (DECL_CXX_DESTRUCTOR_P (decl))
3688 warning_n (DECL_SOURCE_LOCATION (decl),
3689 OPT_Wsuggest_final_methods, count,
3690 "Declaring virtual destructor of %qD final "
3691 "would enable devirtualization of %i call "
3692 "executed %lli times",
3693 "Declaring virtual destructor of %qD final "
3694 "would enable devirtualization of %i calls "
3695 "executed %lli times",
3696 DECL_CONTEXT (decl), count, dyn_count);
3697 else
3698 warning_n (DECL_SOURCE_LOCATION (decl),
3699 OPT_Wsuggest_final_methods, count,
3700 "Declaring method %qD final "
3701 "would enable devirtualization of %i call "
3702 "executed %lli times",
3703 "Declaring method %qD final "
3704 "would enable devirtualization of %i calls "
3705 "executed %lli times",
3706 decl, count, dyn_count);
3710 delete (final_warning_records);
3711 final_warning_records = 0;
3714 if (dump_file)
3715 fprintf (dump_file,
3716 "%i polymorphic calls, %i devirtualized,"
3717 " %i speculatively devirtualized, %i cold\n"
3718 "%i have multiple targets, %i overwritable,"
3719 " %i already speculated (%i agree, %i disagree),"
3720 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3721 npolymorphic, ndevirtualized, nconverted, ncold,
3722 nmultiple, noverwritable, nspeculated, nok, nwrong,
3723 nexternal, nnotdefined, nartificial, ndropped);
3724 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3727 namespace {
3729 const pass_data pass_data_ipa_devirt =
3731 IPA_PASS, /* type */
3732 "devirt", /* name */
3733 OPTGROUP_NONE, /* optinfo_flags */
3734 TV_IPA_DEVIRT, /* tv_id */
3735 0, /* properties_required */
3736 0, /* properties_provided */
3737 0, /* properties_destroyed */
3738 0, /* todo_flags_start */
3739 ( TODO_dump_symtab ), /* todo_flags_finish */
3742 class pass_ipa_devirt : public ipa_opt_pass_d
3744 public:
3745 pass_ipa_devirt (gcc::context *ctxt)
3746 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3747 NULL, /* generate_summary */
3748 NULL, /* write_summary */
3749 NULL, /* read_summary */
3750 NULL, /* write_optimization_summary */
3751 NULL, /* read_optimization_summary */
3752 NULL, /* stmt_fixup */
3753 0, /* function_transform_todo_flags_start */
3754 NULL, /* function_transform */
3755 NULL) /* variable_transform */
3758 /* opt_pass methods: */
3759 virtual bool gate (function *)
3761 /* In LTO, always run the IPA passes and decide on function basis if the
3762 pass is enabled. */
3763 if (in_lto_p)
3764 return true;
3765 return (flag_devirtualize
3766 && (flag_devirtualize_speculatively
3767 || (warn_suggest_final_methods
3768 || warn_suggest_final_types))
3769 && optimize);
3772 virtual unsigned int execute (function *) { return ipa_devirt (); }
3774 }; // class pass_ipa_devirt
3776 } // anon namespace
3778 ipa_opt_pass_d *
3779 make_pass_ipa_devirt (gcc::context *ctxt)
3781 return new pass_ipa_devirt (ctxt);
3784 #include "gt-ipa-devirt.h"